yohan sucipto yohan sucipto - 9 days ago 6
Java Question

How do I fill the email section using chrome driver

enter image description here

Can you help me to input text on the email box? The email box appears when I click on

masuk
button on the top, but I can't get to
sendkeys
on the email box.

H|ere is the url- www.tokopedia.com

And here is the code that does not work

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

Answer

This is because authorization form located inside iframe element. You need to switch to that frame at first and then handle input fields:

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

To switch back you might need to use

driver.switchTo().defaultContent();

P.S. You don't need to click on input field to sent text to it, so driver.findElement(By.id("inputEmail")).click(); is redundant line