Java Selenium WebDriver Can't find form field

2019-05-19 15:18发布

I'm testing a registration page and I've tried name, xpath, id, class and nothing seems to work.

here is my selenium code

  driver.findElement(By.id("pushMenu")).click();
    Thread.sleep(2000);
    driver.findElement(By.linkText("Register")).click();
    Thread.sleep(2000);
    WebElement l = driver.findElement(By.name("name"));

and here is the form tag I'm trying to access

<input type="text" name="name" placeholder="">

explicit wait attempt code:

 WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.name("name")));
    driver.findElement(By.name("name")).sendKeys("Testing100");

4条回答
SAY GOODBYE
2楼-- · 2019-05-19 15:30

Please check the time specified in the wait is sufficient for the element to load completely.
OR
Use different locators like cssSelector if name, id(if present) is not working.

CssSelector example
driver.findElement(By.cssSelector(input[name='name']));
查看更多
啃猪蹄的小仙女
3楼-- · 2019-05-19 15:31

For accessing the form field, use

driver.findElement(By.xpath("//input[@name='name']"));

查看更多
神经病院院长
4楼-- · 2019-05-19 15:41

If you are still facing the issue of accessing the element after implicit/explicit wait, then try putting a screenshot before you access the element and see if the element is present on the page or not.

查看更多
混吃等死
5楼-- · 2019-05-19 15:43

in HTML u shared there is no name tag. so try by.linktext("Manager")

查看更多
登录 后发表回答