I use the code
driver.findElement(By.name("username")).sendKeys("name");
driver.findElement(By.name("password")).sendKeys("12345");
to login to a website. Sometimes it will work, sometimes it won’t, the error given is
Exception in thread “main”
org.openqa.selenium.NoSuchElementException:
Unable to locate element:
{“method”:”name”,”selector”:”username”}
The problem is that sometimes it will work. Should I perhaps use Byxpath()?
You can use WebDriverWait with conditions to wait for Elements:
With NameExpectedCondition:
}
Please make also sure you’re searching for the element in the correct frame.