【问题标题】:Wait to perform two actions - selenium/ java等待执行两个动作 - selenium/java
【发布时间】:2018-08-07 08:33:05
【问题描述】:

我正在尝试使用 Fluent 等待执行如下两个操作:

  1. 点击搜索按钮
  2. 检查元素的结果

现在我正在尝试使用以下代码,但它似乎不起作用:

 public SendMailPage waitForSometime() throws Exception {

    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofMinutes(2))
            .pollingEvery(Duration.ofSeconds(10))
            .ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {

        public WebElement apply(WebDriver driver) {
            driver.findElement(By.xpath("//BUTTON[@type='submit'][text()='Search']")).click();

            driver.findElement(By.xpath("xpath of the element i'm waiting to find"));

            return driver.findElement(By.xpath("xpath of the element i'm waiting to find"));
        }

    });

    element.isDisplayed();

    return new SendMailPage();
}

有人可以指导我如何解决这个问题吗?

***更新代码:等待单个元素也不起作用:

public SendMailPage assertMailSubject() throws Exception {
    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
            .withTimeout(Duration.ofMinutes(2))
            .pollingEvery(Duration.ofSeconds(30))
            .ignoring(NoSuchElementException.class);

    WebElement element = wait.until(new Function<WebDriver, WebElement>() {

          public  WebElement apply(WebDriver driver) {


         return driver.findElement(By.xpath("the element that i am waiting for"));
                                        }
                                    }
    );
    return new SendMailPage();
}

【问题讨论】:

  • 将它们中的每一个分隔成单独的wait.until 调用。
  • 出于某种原因,即使有一个wait.until,代码也不会等待。
  • 请发布更新后的代码。
  • 我已经更新了OP。
  • 我相信你的等待是成功的。你可以为元素添加一个sop并检查吗?可能是你在等待一个错误的元素。

标签: selenium selenium-webdriver webdriver ui-automation fluentwait


【解决方案1】:

我解决了这两个问题:

  1. 代码无法正常工作,因为 NoSuchElementException 来自 Java util 而不是 Selenium。

  2. 为了执行两个动作,我只是在 return 语句之前添加了 Search key action。

【讨论】:

    猜你喜欢
    • 2017-02-13
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多