Selenium webdriver Selenium Webdriver:NoTouchElementException

Selenium webdriver Selenium Webdriver:NoTouchElementException,selenium-webdriver,nosuchelementexception,Selenium Webdriver,Nosuchelementexception,我被困在上面有一段时间了,所以我想我应该寻求帮助。 我正在尝试使用页面工厂机制(使用内置findelementapi)按名称查找元素。元素在网页上,但仍然找不到它。谁能指出我哪里做错了?谢谢 @FindBy(how = How.NAME, using = "userName") private WebElement userName; public WebElement getUserName() { return userName; } homepage.getUserName()

我被困在上面有一段时间了,所以我想我应该寻求帮助。 我正在尝试使用页面工厂机制(使用内置findelementapi)按名称查找元素。元素在网页上,但仍然找不到它。谁能指出我哪里做错了?谢谢

@FindBy(how = How.NAME, using = "userName")
private WebElement userName;

public WebElement getUserName() {
    return userName;
}

homepage.getUserName().sendKeys("test");

你的代码看起来不错。但是,您可能需要添加sendKeys之前的等待,因为此时可能没有加载页面

WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
wait.Until(By.Id("login"));

希望有帮助

添加有关错误日志的更多信息:org.openqa.selenium.NoSuchElementException:无法定位元素:{“方法”:“名称”,“选择器”:“用户名”}命令持续时间或超时:16毫秒有关此错误的文档,请访问:生成信息:版本:'2.53.1',修订版:'A36B8B1CD57287168E54B817830ADCE9B0158D',时间:'2016-06-30 19:26:09'谢谢Sakshi。添加等待帮助我找到了根本原因。在添加wait之后,我可以看到它试图在空页面上查找元素,因为junit测试是以不确定的顺序运行的:)我通过使测试彼此独立来修复它。我现在以一个干净的状态开始每个测试(通过从开始为每次运行打开网站),现在一切正常。我还发现,在JUnit 4.1.2中,我们现在可以使用“@FixMethodOrder”控制测试的运行顺序,但我不希望这样做,因为测试应该独立运行。@Tajinder:如果这回答了您的问题,请将其标记为答案