[英]Selenium Fluent Wait Implementation in the code still gives “org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:”
The mentioned URL in the code takes 5 seconds to display the login screen and in order to enter the details on the login page, I have implemented fluent wait for 10 seconds in my code.代码中提到的 URL 需要 5 秒才能显示登录屏幕,为了在登录页面上输入详细信息,我在代码中实现了流畅的等待 10 秒。 Even though the wait is correctly mentioned, for some reason this wait is not obeyed and i am always displayed with org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:即使正确提到了等待,由于某种原因,这种等待没有被遵守,我总是显示 org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:
CODE:代码:
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));
}
}
Error:错误:
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
To send a character sequence within the Email address field you have to induce WebDriverWait for the elementToBeClickable()
and you can use either of the following Locator Strategies :要在Email 地址字段中发送字符序列,您必须为elementToBeClickable()
引入WebDriverWait ,并且可以使用以下任一定位器策略:
Using cssSelector :使用cssSelector :
driver.get("https://app.hubspot.com/login"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#username"))).sendKeys("Bimlesh@gmail.com");
Using xpath :使用xpath :
driver.get("https://app.hubspot.com/login"); new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='username']"))).sendKeys("Bimlesh@gmail.com");
Browser Snapshot:浏览器快照:
You can find a couple of detailed discussions on NoSuchElementException in:您可以在以下位置找到关于NoSuchElementException的一些详细讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.