硒-等待几分钟,然后单击按钮

时间:2019-09-24 21:54:08

标签: selenium ui-automation

页面加载后,我想等待几分钟,然后单击某个按钮。 试图做这样的事情:

{
  "query": {
    "bool": {
      "should": [
        {
          "exists": {
            "field": "market"
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "market"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

但是它不起作用。测试仍然可以执行,而无需等待几分钟。我错过了什么吗?

2 个答案:

答案 0 :(得分:0)

Thread.Sleep()在这里被评论了吗?

如果要始终等待几分钟,则需要使用显式等待-Thread.Sleep(50000));Thread.Sleep(TimeSpan.FromSeconds(5));

隐式等待通常在等待元素存在的上下文中使用。但是由于您总是想等待几分钟,因此应该使用显式等待。

答案 1 :(得分:0)

您可以结合使用WebDriverWaitExpectedConditions来实现,然后利用.elementToBeClickable方法,它将首先等待,然后单击。

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("...."))).click();

正在导入:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;

10超时(以秒为单位),然后尝试使用您的定位器更改By.xpath("....")

Docs