簡體   English   中英

代碼中的 Selenium Fluent Wait Implementation 仍然給出“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: 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");

瀏覽器快照:

Hubsopt


參考

您可以在以下位置找到關於NoSuchElementException的一些詳細討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM