即使切换到Selenium WebDriver,也无法在iframe中定位元素 [英] Selenium WebDriver can't locate element in iframe even when switching to it

查看:69
本文介绍了即使切换到Selenium WebDriver,也无法在iframe中定位元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了很多论坛主题来解决我的问题,但没有发现任何类似的问题.我正在使用Java使用Selenium WebDriver编写测试.

I have searched a lot of forum topîcs to solve my issue but I didn't find any similar problem. I'm writing tests with Selenium WebDriver using Java.

我正在测试的网站使用iframe,该用户的所有可见内容都将加载到该iframe中.看起来像这样:

The website I'm testing uses an iframe where all the visible content for the user will be loaded into. It looks like something like this :

<html>
  <head>
     (...)  
  </head>
  <body>
    <iframe id="portalIframe">  
      <html>
        <body>
          <h2 id="aui_3_2_0_1594">Etat des agents</h2>
          ...
        </body>
      </html>       
    </iframe>       
  </body>
</html>

我的问题如下:在iframe中查找元素并不总是有效.当我想找到这样的元素时,我首先运行一个方法以切换到iframe:

My problem is the following : finding an element in the iframe doesn't always work. When I want to locate such an element I first run a method to switch to the iframe :

protected void switchIframe() {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    By by = By.id("portalIframe");
    try {
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(by));
} catch (TimeoutException e) {
    e.printStackTrace();
    }
}

然后我可以例如找到一个h2元素:

And then I can for example find a h2 element:

 driver.findElement(By.xpath("//h2[contains(text(),'Some text')]"));

我无法理解的是,硒有时无法在网页上找到元素.例如,我只是设法找到h2元素,然后单击菜单链接以加载另一个页面,但是当我尝试再次找到<时,在新页面上的h2>元素上,出现找不到元素错误",如果我尝试在此之前添加switchIframe(),则硒表示找不到iframe.

What I can't understand is that sometimes selenium doesn't manage to find elements on the webpage. For instance I just managed to find the h2 element, then I click on a menu link to load another page but when I try to find again a < h2 > element on the new page, I get a "element not found error" and if I try to add switchIframe() before that, selenium says it cannot find the iframe.

这似乎是随机"发生的,所以我真的不知道该怎么做才能解决这个问题.ChromeDriver和FirefoxDriver上的问题相同.我不使用其他驱动程序,因为此私有网站仅使用Firefox和Chrome.

It seems to happen "randomly" so I don't really know what to do to solve this issue. The issue is the same on ChromeDriver and FirefoxDriver. I don't use others drivers because only Firefox and Chrome will be used for this private website.

编辑抱歉,我无法粘贴html内容,因为它是一个Intranet网站,我无法在此处共享.我尝试找到的h2元素的示例<h2 id ="aui_3_2_0_1594">代理商代理商</h2>

EDIT I'm sorry I can't paste the html content because it's a intranet website and I can't share it here. An example of a h2 element I try to locate < h2 id="aui_3_2_0_1594">Etat des agents< /h2 >

我的问题也与此问题非常相似:

Also my problem is quite similar to this issue : Selenium WebDriver not able to find elements which are not present in the Page Source but present in HTML when seen through Developer Tools The issue appens not only on h2 elements but also for example on "a" elements I want to click on.

谢谢

推荐答案

在寻找html元素时,我似乎没有等待足够的时间.我只是增加了我的显式等待方法的超时.我不是在谈论 switchIframe(),而是在谈论另一种用于等待元素出现的方法:

It appeared that I wasn't waiting enough time when looking for html elements. I just increased the timeout of my explicit wait method. I'm not talking about switchIframe() but I'm referring to another method used to wait for elements to be present :

因此,我将超时时间从3秒增加到了6秒,现在看来效果很好.

So, I've increased the timeout from 3 to 6 seconds and it seems to work great now.

protected WebElement waitForElementVisible(By by) {
    WebDriverWait wait = new WebDriverWait(driver,6);
    WebElement element = null;

    try {
        element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (TimeoutException e) {
        //method logging an error
        error("Timeout : element " + by.toString() + " is not visible");
    }
    return element;
}

这篇关于即使切换到Selenium WebDriver,也无法在iframe中定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆