![](/img/trans.png)
[英]org.openqa.selenium.NoSuchElementException: Unable to locate element error
[英]Selenium Fluent Wait Implementation in the code still gives “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:”
代码中提到的 URL 需要 5 秒才能显示登录屏幕,为了在登录页面上输入详细信息,我在代码中实现了流畅的等待 10 秒。 即使正确提到了等待,由于某种原因,这种等待没有被遵守,我总是显示 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:
代码:
public class FluentWaitDemo {
public static void main(String[] args) throws InterruptedException
{
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://app.hubspot.com/login");
By email = By.xpath("//input[@type='email']");
WebElement userId = FluentWaitForElement(driver, email);
userId.sendKeys("*******@gmail.com");
driver.close();
}
public static WebElement FluentWaitForElement(WebDriver driver, By locator)
{
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(10))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
return wait.until(ExpectedConditions.presenceOfElementLocated(locator));
}
}
错误:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='email']"}
(Session info: chrome=83.0.4103.97)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
要在Email 地址字段中发送字符序列,您必须为elementToBeClickable()
引入WebDriverWait ,并且可以使用以下任一定位器策略:
使用cssSelector :
driver.get("https://app.hubspot.com/login"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#username"))).sendKeys("Bimlesh@gmail.com");
使用xpath :
driver.get("https://app.hubspot.com/login"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='username']"))).sendKeys("Bimlesh@gmail.com");
浏览器快照:
您可以在以下位置找到关于NoSuchElementException的一些详细讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.