代码中的Selenium Fluent Wait实现仍然给出“ org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:”

问题描述 投票:0回答:1

代码中提到的URL需要5秒钟才能显示登录屏幕,并且为了在登录页面上输入详细信息,我在代码中实现了流利的等待10秒钟。即使正确地提到了等待,但由于某种原因,也没有遵守该等待,并且我总是与org.openqa.selenium.NoSuchElementException一起显示:没有这样的元素:无法找到元素:

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));
    }
}

错误:

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
java selenium xpath css-selectors nosuchelementexception
1个回答
0
投票

要在电子邮件地址字段中发送字符序列,您必须为WebDriverWait引入element_to_be_clickable(),并且可以使用以下Locator Strategies中的任何一个:

  • 使用cssSelector

    driver.get("https://app.hubspot.com/login");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input#username"))).sendKeys("[email protected]");
    
  • 使用xpath

    driver.get("https://app.hubspot.com/login");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='username']"))).sendKeys("[email protected]");
    

浏览器快照:

Hubsopt


参考

您可以在NoSuchElementException中找到一些详细的讨论:

© www.soinside.com 2019 - 2024. All rights reserved.