Selenium:如何使 WebDriver 忽略“元素不可见”?错误?

发布于 2024-12-03 14:16:49 字数 477 浏览 3 评论 0原文

我正在使用 Selenium WebDriver (v2.5.0)。当我使用 driver.click(...)" 命令时出现此错误

Element is not currently visible and so may not be interacted with Build info: version: '2.5.0', revision: '13516', time: '2011-08-23 18:30:44' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.38-10-generic', java.version: '1.6.0_26' Driver info: driver.version: RemoteWebDriver

在浏览器中,当我将鼠标悬停在某个元素上时,被单击的元素变得可见。有什么方法可以检查是否存在有些东西是可见的还是不可见的?

I'm using Selenium WebDriver (v2.5.0). I get this error when I use a driver.click(...)" command

Element is not currently visible and so may not be interacted with Build info: version: '2.5.0', revision: '13516', time: '2011-08-23 18:30:44' System info: os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.38-10-generic', java.version: '1.6.0_26' Driver info: driver.version: RemoteWebDriver

In the browser when I mouse hover on an element, the element being clicked becomes visible. Is there any way to check whether something is visible or not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洛阳烟雨空心柳 2024-12-10 14:16:49

你可以通过行动来做到这一点。实现你想要的,使用Python Webdriver客户端,但是原理是一样的。

ActionChains(driver).move_to_element(driver.find_element(By.ID, 'Foo'))\
  .click(driver.find_element(By.Name, "Bar"))\
  .perform()

You can do it via actions. To achieve what you want, using the Python Webdriver client, but the principle is the same.

ActionChains(driver).move_to_element(driver.find_element(By.ID, 'Foo'))\
  .click(driver.find_element(By.Name, "Bar"))\
  .perform()
念﹏祤嫣 2024-12-10 14:16:49

最好的解决方案是不使用 click() 方法,而是实现 Actions 并让 selenium (通过 webdriver)模拟鼠标在元素上移动以激活事件,然后使元素可单击/启用。激活该元素后,然后根据需要执行 click() 方法。我假设该元素已被禁用,使其首先无法单击。

建立您的元素,您还可以使用 RenderedWebElement ,它具有悬停()方法,然后您不需要创建以下 Actions 对象,但它可能无法工作,具体取决于应用程序如何使用本机事件设计。两者都尝试一下,看看哪个效果最好、最优雅。

WebElement element = driver.findElement(By.id("element_id"));

创建新的操作 由网络驱动程序支持的对象

Actions actions = new Actions(driver);

将光标移动到元素 - 这将“激活”您的元素以使其可单击

actions.moveToElement(element).perform();

验证该元素现在可单击或“已启用”

assertThat(element.isEnabled());

现在执行单击操作

element.click();

Your best solution is to not use the click() method but to implement Actions and have selenium (via webdriver) simulate the mouse moving over the element to activate the events which then make the element clickable/enabled. once you have activated the element, then perform the click() method as needed. I am assuming that the element is disabled making it not clickable in the first place.

Establish your element you could also use RenderedWebElement which has a hover() method then you wouldn't need to create the following Actions object however it may not work depending on how the application is designed with native events. Try both see which works best and is most elegant.

WebElement element = driver.findElement(By.id("element_id"));

Create a new actions object backed by the webdriver

Actions actions = new Actions(driver);

Move the cursor to the element - this will "activate" your element to be clickable

actions.moveToElement(element).perform();

Verify the element is now clickable or "enabled"

assertThat(element.isEnabled());

Now perform the click action

element.click();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文