首页 > 解决方案 > Selenium Web Driver FindElement() 方法不起作用(NoSuchElementException)

问题描述

所以我的问题很简单。这是我的代码:

    driver.findElement(By.id("j_username")).sendKeys("nk");
    driver.switchTo().frame(0);
    driver.findElement(By.id("j_password")).sendKeys("1");

第一个 findElement() 方法可以正常工作,但是当我第二次尝试使用它时,我得到了这个错误:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"j_password"}

所有 id 都是正确的。

标签: javaseleniumselenium-webdriver

解决方案


试试这个:

driver.findElement(By.id("j_username")).sendKeys("nk");
driver.findElement(By.name("j_password")).sendKeys("1"); // find by name

第二个元素没有id,至少在HTML你提供的。


推荐阅读