2016-11-29 61 views
0

enter image description here如何填充使用電子郵件部分鉻司機

你能幫我的郵箱輸入的文本?當我點擊頂部的masuk按鈕時,電子郵件框會出現,但我無法在電子郵件框中找到sendkeys

H | ERE是URL-www.tokopedia.com

這裏是行不通

public static void main(String[] args) throws Exception { 
System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe"); 
WebDriver driver=new ChromeDriver(); 
driver.get("https://www.tokopedia.com/"); 
Thread.sleep(3000); 
WebElement element = driver.findElement(By.id("login-ddl-link")); 
JavascriptExecutor executor = (JavascriptExecutor)driver; 
executor.executeScript("arguments[0].click();", element); 
driver.findElement(By.id("login-ddl-link")).click(); 
driver.switchTo().frame("iframe-accounts"); 
WebElement myEmail = driver.findElement(By.id("inputEmail")); 
myEmail.sendKeys("tes213"); 
WebElement myPassword = driver.findElement(By.id("inputPassword")); 
myPassword.sendKeys("tes123"); 
} 
+0

你是什麼意思'不起作用?你的代碼執行的結果是什麼? – Andersson

+0

它只點擊masuk按鈕,但不會點擊電子郵件框,並不會填充它 –

回答

0

這是因爲授權表位於iframe元素中的代碼。您需要先切換到該幀,然後處理輸入字段:

... 
driver.findElement(By.id("login-ddl-link")).click(); 
Thread.sleep(2000); 
driver.switchTo().frame("iframe-accounts"); 
WebElement myEmail = driver.findElement(By.id("inputEmail")); 
myEmail.sendKeys("tes123"); 
... 

要切換回您可能需要使用

driver.switchTo().defaultContent(); 

附:你不需要點擊輸入字段發送文本,所以driver.findElement(By.id("inputEmail")).click();是多餘的線

+0

嗨那裏你的代碼就像雙擊ifram帳戶,所以iframe關閉立即 –

+0

你確定你不使用'驅動程序。 findElement(By.id(「login-ddl-link」))。click();'你的代碼中有兩次?你必須插入一行'driver.switchTo().frame(「iframe-accounts」);'到你的代碼 – Andersson

+0

是的,我沒有把driver.findElement(By.id(「login-ddl-link」))。點擊();兩次 –

0

使用此代碼它在Chrome上工作得很好。

public static void main(String [] ar) throws Exception { 

     System.setProperty("webdriver.chrome.driver", "E:\\Download\\chromedriver_win32\\chromedriver.exe"); 
     WebDriver driver=new ChromeDriver(); 
     driver.get("https://www.tokopedia.com/"); 

     Thread.sleep(3000); 

     WebElement element = driver.findElement(By.xpath("//*[@id='login-ddl-link']")); 

     JavascriptExecutor executor = (JavascriptExecutor)driver;   
     executor.executeScript("arguments[0].click();", element); 

     driver.switchTo().frame("iframe-accounts"); 

     WebElement myEmail = driver.findElement(By.id("inputEmail")); 
     myEmail.sendKeys("tes213"); 

     WebElement myPassword = driver.findElement(By.id("inputPassword")); 
     myPassword.sendKeys("tes123"); 

     driver.findElement(By.xpath(".//*[@id='global_login_btn']")).click(); 
} 
相關問題