代码中的 Selenium Fluent Wait 实现仍然给出 "org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element:"

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:没有这样的元素:无法定位元素:

代码:

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

要在 电子邮件地址 字段中发送 字符序列 ,您必须引入 for the elementToBeClickable() and you can use either of the following :

  • 使用 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");
    

浏览器快照:


参考

您可以在以下位置找到关于 的一些详细讨论: