【问题标题】:How to wait till text value is fully loaded in selenium如何等到文本值在硒中完全加载
【发布时间】:2021-06-20 15:15:33
【问题描述】:

我在 Selenium 中尝试了以下代码来获取行值。

    public static void main(String[] args) throws InterruptedException {
    WebDriverManager.chromedriver().setup();
    ChromeDriver driver = new ChromeDriver();
    driver.get("https://www2.asx.com.au/");

    driver.manage().window().maximize();
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    try {

        WebElement cookies = driver.findElementByXPath("//button[text()='Accept All Cookies']");
        wait.until(ExpectedConditions.elementToBeClickable(cookies)).click();

    } catch (Exception e) {
        System.out.println("cookies pop up not found");
    }

    WebElement el = driver
            .findElementByXPath("//*[@class='markit-home-top-five aem-GridColumn aem-GridColumn--default--12']");

    wait.until(ExpectedConditions.visibilityOf(el));

    ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", el);
    List<WebElement> elements = driver.findElementsByXPath("//caption[text()='Gains']//ancestor::table//tr");

    for (int i = 1; i <= elements.size(); i++) {

        Thread.sleep(2000);

        System.out.println(driver.findElementByXPath(
                "//caption[text()='Gains']//ancestor::table//tr[" + i + "]//span[@class='value-with-arrow']")
                .getText());
    }

}

我遇到了两个问题:

  1. “允许所有 cookie”按钮没有被点击,弹出窗口仍然存在。 'cookies pop up not found' 正在输出中打印出来。
  2. 没有睡眠就不会打印表格的值。在下面的输出中,显示第一个文本值打印为 -- 因为该值仍在加载。如何提供直到价值完全加载。

提前致谢!

【问题讨论】:

    标签: java selenium cookies automation webdriverwait


    【解决方案1】:
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("element_id"), "The Text"));
    

    使用 webdriver wait ,您可以使用预期的条件 texttobepresent 或 element to be present

    https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html#textToBePresentInElementLocated(org.openqa.selenium.By,java.lang.String)

    【讨论】:

    • 感谢您的指点。知道为什么弹出的 cookie 不起作用吗?
    猜你喜欢
    • 1970-01-01
    • 2011-03-17
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-11
    • 2020-08-29
    • 1970-01-01
    相关资源
    最近更新 更多