Selenium Webdriver:NoSuchElementException

Selenium Webdriver: NoSuchElementException

我现在被困了一段时间,所以我想我应该寻求帮助。 我正在尝试使用内置 findElement API 中使用的页面工厂机制按名称定位元素。元素在网页上,但仍然找不到。谁能指出我做错了什么?谢谢

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

希望对您有所帮助!