【问题标题】:How do I make Selenium webdriver to wait for an element to change its attribute to something else in Java如何使 Selenium webdriver 等待元素将其属性更改为 Java 中的其他内容
【发布时间】:2018-09-25 13:22:37
【问题描述】:

下面是我的元素;

String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");

由于现在属性值存储在“sId”中,我现在需要告诉 Selenium 等到 data-id 属性值 不等于 到 sID。 我试过下面的代码没有运气:

wait.until_not(ExpectedConditions.textToBePresentInElement(By.xpath(path), sId));

我得到错误:

方法 until_not(ExpectedCondition) 未定义 键入 WebDriverWait

即使我将“until_not”更改为“until”,我也会收到以下警告:

来自类型的方法 textToBePresentInElement(By, String) ExpectedConditions 已弃用

我该怎么做?

【问题讨论】:

  • 等到 data-id attribute value is NOT equal to sID 将是误报。 IMO,如果 usecase 允许,您应该等待value is equal to sID

标签: java selenium selenium-webdriver webdriver selenium-chromedriver


【解决方案1】:

如果您正在检查属性值,检查文本对您没有帮助,那是两件不同的事情。

您可以为此组合notattributeToBe ExpectedConditions

wait.until(ExpectedConditions.not(ExpectedConditions.attributeToBe(By.xpath(path), "data-id", sId)));

【讨论】:

    【解决方案2】:

    您可以尝试使用 not from Expected 条件,例如:

    wait.until(ExpectedConditions.not(ExpectedConditions.textToBePresentInElement(<element>, <elementtext>)));
    

    或者你也可以使用预期条件隐形方法:

    wait.until(ExpectedConditions.invisibilityOfElementWithText(locator, text));
    

    【讨论】:

      【解决方案3】:

      你可以试试下面的代码

      String sId = driver.findElement(By.xpath(path)).getAttribute("data-id");        
      
      boolean expectedCondition = false;
      boolean timeOut = false;
      int second = 1;
      do {
          try {
              //timeout for 60 seconds
              if(second>60) {
                  timeOut = true;
              }
              String expectedId = driver.findElement(By.xpath(path)).getAttribute("data-id");
              if(! expectedId.equals(sId)) {
                  expectedCondition=true;
              }else {
                  second++;
              }
          } catch (Exception e) {
              TimeUnit.SECONDS.sleep(1);
              second++;
          }
      }while(expectedCondition==false && timeOut==false);
      

      【讨论】:

        猜你喜欢
        • 2017-10-04
        • 2016-12-25
        • 2022-01-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多