【问题标题】:Need to wait for finding specific element on loading webpage - selenium 3.141.5 and java 8需要等待在加载网页上找到特定元素 - selenium 3.141.5 和 java 8
【发布时间】:2026-02-02 11:35:01
【问题描述】:

使用 selenium 3.141.5 (Latest) 和 java 8。现在我需要等待网页上的特定元素在执行下一行之前加载。我正在尝试使用 ExpectedConditions java 类,但无法导入它。在 selenium 的 javadoc 中,我可以找到 ExpectedConditions 和 ExpectedCondition。 [PSB]

static ExpectedCondition<WebElement> presenceOfElementLocated(By locator)

检查一个元素是否存在于页面的 DOM 上的期望。

我没有使用任何 maven 或任何其他工具。它只是eclipse、java和selenium。 Image from my local eclipse

请帮忙。我只想等待特定元素被加载,然后再使用最新的 selenium 和 java 执行我的下一行代码。提前致谢! :) 我希望我已经尽力解释好,如果不是那么抱歉

【问题讨论】:

    标签: java java-8 selenium3


    【解决方案1】:

    我正在使用 ExpectedConditions

    // modified wait method
    public WebDriverWait wait_sec(WebDriver driver, int sec) {return new WebDriverWait(driver, sec);}
    
    // example of usage one of ExpectedConditions
    driver.get(url_portal);
    WebElement fld_pwd = wait_sec(driver, 60).until(ExpectedConditions.elementToBeClickable(By.name("password")));
    fld_pwd.click();
    fld_pwd.sendKeys(sec_var.getPwd());
    
    // example of negative usage
    wait_sec(driver, 300).until(ExpectedConditions.not(ExpectedConditions.urlContains("#")));
    

    探索 ExpectedConditions 对我的测试非常重要。

    【讨论】:

    • 4.3.8.RELEASE3.8.14.12
    【解决方案2】:

    我切换到 selenium 3.11,现在一切正常!在那里我可以使用 ExpectedConditions

    谢谢

    【讨论】: