如何使用chrome驱动程序填写电子邮件部分

时间:2016-11-29 10:15:32

标签: java google-chrome selenium selenium-webdriver webdriver

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");
}

2 个答案:

答案 0 :(得分: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();

P.S。您无需单击输入字段即可向其发送文本,因此driver.findElement(By.id("inputEmail")).click();为冗余行

答案 1 :(得分: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();
 }