【问题标题】:Switch to iframe and click on element doesn't work and get StaleElementReferenceException切换到 iframe 并单击元素不起作用并获得 StaleElementReferenceException
【发布时间】:2019-11-23 02:21:20
【问题描述】:

我正在这个网站上练习:https://dojotoolkit.org/reference-guide/1.9/dijit/layout/TabContainer-examples.html

  1. 使用以下方法单击编程嵌套选项卡部分中的运行按钮:

        WebElement productElement = null;
        List<WebElement> productElements= driver.findElements(By.cssSelector(div.section));
    
        for(int i=0;i<productElements.size();i++)
        {
            String text = productElements.get(i).findElement(By.tagName("h2")).getText();
            if (text.equalsIgnoreCase(tabName)){
                productElement = productElements.get(i);
                break;
            }
        }
        return productElement;
    }
    public void clickRunButton(String tabName) {
        WebElement programmaticNestedtabs = findTab(tabName);
        WebElement runButton = programmaticNestedtabs.findElement(By.cssSelector("a.CodeGlassMiniRunner"));
        runButton.click();
    }
    
  2. 弹出屏幕需要一段时间才能加载。然后我尝试点击 Tab 2:

WebDriverWait wait = new WebDriverWait(driver, 50);
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
dialog.findElement(By.xpath("//div[@class='dijitTabListWrapper dijitTabContainerTopNone dijitAlignCenter']//div[2]")).click();```

I got the StaleElementReferenceException when I run the code.


【问题讨论】:

    标签: java selenium iframe staleelementreferenceexception


    【解决方案1】:

    StaleElementReferenceException 通常在元素尚未附加到 DOM 并且您尝试与之交互时抛出。您可以通过应用等待条件来绕过它,直到元素准备好可点击为止。

    wait.until(ExpectedConditions.elementToBeClickable(WebElement));
    

    【讨论】:

      【解决方案2】:

      我已经编写了一个通用包装器来处理 iFrame 的类似异常

      private ExpectedCondition<Boolean> frameToBeAvailableAndSwitchToIt(final WebElement var0) {
          return new ExpectedCondition<Boolean>() {
              public Boolean apply(WebDriver var1) {
                  try {
                      if (var0).isDisplayed()) {
                          var1.switchTo().frame(var0);
                          return true;
                      }
                  } catch (NoSuchFrameException var3) {
                      return false;
                  } catch (NoSuchElementException var4) {
                      return false;
                  } catch (StaleElementReferenceException var5) {
                      return false;
                  }
                  return false;
              }
      
              public String toString() {
                  return "frame to be available: " + var0;
              }
          };
      }
      

      你可以这样称呼它

      WebDriverWait wait = new WebDriverWait(driver, waitTimeOutInSeconds, pollTimeOutInMillis);
          wait.until(frameToBeAvailableAndSwitchToIt(iFrameWebElement));
      

      【讨论】:

        猜你喜欢
        • 2014-01-21
        • 1970-01-01
        • 1970-01-01
        • 2015-11-11
        • 1970-01-01
        • 2021-07-09
        • 2020-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多