水豚,硒与隐藏或显示交互:无CSS属性

发布于 2024-12-10 08:25:48 字数 542 浏览 1 评论 0原文

我使用类似的结构:

<div class="edit" style="visibility: hidden;">
  <a href="some_path" id="edit_item">Edit</a>
</div>

然后我将鼠标悬停在这个元素上变得可见,但是我很难将此操作与测试交互(使用黄瓜,水豚,硒)。

我收到一个错误

元素当前不可见,因此可能无法与之交互(Selenium::WebDriver::Error::ElementNotDisplayedError)

我尝试使用 Element.trigger(event) 鼠标悬停时,但它在硒中不起作用...... 我如何与这个元素交互?

I'm using similar construction:

<div class="edit" style="visibility: hidden;">
  <a href="some_path" id="edit_item">Edit</a>
</div>

Then I hover mouse this element become visible, but I have difficult to interact this actions with tests(using cucumber, capybara, selenium).

I get an error

Element is not currently visible and so may not be interacted with (Selenium::WebDriver::Error::ElementNotDisplayedError)

I tried to use Element.trigger(event) with mouseover, but it doesn't work in selenium...
How I can interact with this element?

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

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

发布评论

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

评论(2

您的好友蓝忘机已上羡 2024-12-17 08:25:48

我使用水豚中的execute_script解决了这个问题:

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').mouseover();")
  end
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').click();")
  end
end

但是这个解决方案不适用于css - display: none;

I solved this problem using execute_script from capybara:

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').mouseover();")
  end
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  with_scope(selector) do
    page.execute_script("$('#{id}').click();")
  end
end

but this solution doesn't work with css - display: none;

风月客 2024-12-17 08:25:48

为了将来参考,请记住 execute_script< /a> 可以在 elements 中调用,它提供了 更好的异步保证

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  element = within(selector) { find_by_id(id) }
  element.execute_script('this.mouseover()')
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  within(selector) { find_by_id(id) }.execute_script('this.click()')
end

指定 visible: :all 是查找隐藏元素的另一种方法:

find('.edit', visible: :all).hover
click_link('Edit')

也就是说,与隐藏元素交互可能会隐藏 UI 中不允许的实际问题用户执行交互,因此最好谨慎使用它。

For future reference, have in mind that execute_script can be called in elements, which provides better async guarantees:

When /^I hover element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  element = within(selector) { find_by_id(id) }
  element.execute_script('this.mouseover()')
end

When /^I click in hide element "([^""]*)"(?: within "([^""]*)")?$/ do |id,selector|
  within(selector) { find_by_id(id) }.execute_script('this.click()')
end

Specifying visible: :all is another way to find hidden elements:

find('.edit', visible: :all).hover
click_link('Edit')

That said, interacting with hidden elements may hide real issues in the UI that won't allow a user to perform an interaction, so it's better to use it sparingly.

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