为什么Selenium找不到图形可见的元素 [英] Why Selenium can't find an element that is graphically visible

查看:99
本文介绍了为什么Selenium找不到图形可见的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium来测试在firefox浏览器上运行的应用程序,有时当我想点击带有Selenium的按钮时,它只是找不到它。
我发现这很奇怪,因为我可以用眼睛看到元素。我已经尝试了多种方法,适用于其他例子:

I'm using Selenium to test an appllication running on a firefox browser, sometimes when i want to click on a button with Selenium, it just don't find it. I find this strange since i can see the element with my eye. I have already tried multiple method that worked for other exemple like :


  1. wait.until(ExpectedConditions.visibilityOfElementLocated(by))

  2. wait.until(ExpectedConditions.presenceOfElementLocated(by));
    (我在路径变量中使用xpath,我确定他是正确的)

  3. 在驱动程序上设置implictlyWait

  4. 放入Thread.sleep(1000)

  1. wait.until(ExpectedConditions.visibilityOfElementLocated(by))
  2. wait.until(ExpectedConditions.presenceOfElementLocated(by)); (i use an xpath in the path variable, and i'm sure he is correct)
  3. Setting a implictlyWait on the driver
  4. Putting Thread.sleep(1000)

然后我再次发布,因为我不明白为什么Selenium看不到元素这是在浏览器上显示的。一个重要的信息可能会帮助你给我一个正确的答案是html DOM是由javascript事件触发的websocket动态生成的。

Then again, i post here because i don't understand why Selenium could not see an element that is diplayed on the browser. An important information that could maybe help you giving me a proper answer is that the html DOM is dynamically generate by websocket triggered by javascript event.

编辑1:

<button type="button" class="btn btn-xs btn-block btn-stroke" id="252_button">
    Delete
</button>

我正在尝试访问的元素。我使用xpath来完成它。

The element i'm trying to access. I use an xpath to do it.

它不在iframe元素中。

It's not in an iframe element.

访问元素i做了这个方法应该找到并点击它给出参数中的xpath:

TO access an element i did this method that is supposed to find and click on it giving the xpath in parameter :

public void findAndClick(String xpath) {
        By by = By.xpath(xpath);
        //wait.until(ExpectedConditions.presenceOfElementLocated(by));
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));

        driver.findElement(by).click();
    }

编辑2:

这里特定的xPath:// div [2] / div / div / div / button
我发现它使用的是Selenium插件。

Here the specific xPath : //div[2]/div/div/div/button I've found it using the Selenium plugins.

编辑3:

我得到的例外是


StaleReferenceElementException:Element在缓存中找不到 - 也许页面在查找后有所变化。

StaleReferenceElementException: Element not found in cache - perhaps the page has change since it was looked up.

我去了页面解释了这个例外,它说:

I went to the page that explain this Exception, it says that :

The element has been deleted entirely.
The element is no longer attached to the DOM.

但它不能,因为我可以看到它并手动点击它。

But it can't be, becasue i can see it and click on it manually.

推荐答案

我想我找到了对我的问题的解释,我读了关于 StaleReferenceException 。更确切地说,在这部分中:

I think i found an explanation of my problem, i read the documention about the StaleReferenceException. More precisely in this part :


元素未附加到DOM

The Element is not Attached to the DOM

用于在Web应用程序中模拟选项卡式UI的常用技术是为每个选项卡准备DIV,但一次只能附加一个,将其余部分存储在变量中。在这种情况下,您的代码完全有可能引用一个不再附加到DOM的元素(也就是说,它的祖先是document.documentElement)。
如果WebDriver在这种情况下抛出过时的元素异常,即使元素仍然存在,引用也会丢失。你应该丢弃你持有的当前引用并替换它,可能是在元素连接到DOM后再次找到它。

A common technique used for simulating a tabbed UI in a web app is to prepare DIVs for each tab, but only attach one at a time, storing the rest in variables. In this case, it's entirely possible that your code might have a reference to an element that is no longer attached to the DOM (that is, that has an ancestor which is "document.documentElement"). If WebDriver throws a stale element exception in this case, even though the element still exists, the reference is lost. You should discard the current reference you hold and replace it, possibly by locating the element again once it is attached to the DOM.

I我认为我处于相同的情况,因为这正是我在webElement的引用上使用方法(getText(),getTag())时我有这个异常,即使这个元素仍然是图形可见的。据我了解,我尝试访问一个不再附加到DOM的引用,因为DOM已经更改。

I think i'm in the same case because this is exactly when i use a method (getText(), getTag()) on the reference of a webElement that i have this exception even if this element is still graphically visible. As i understand it, i try to access a reference which is no longer attached to the DOM since the DOM have changed.

这篇关于为什么Selenium找不到图形可见的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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