动作生成器可以解决悬停元素吗?
can action builder solve hovered elements?
目前,我经常将鼠标悬停在页面上的元素变为可见。
action builder 听起来很有前途,但似乎需要事先找到元素,而不是在操作过程中。
这行不通...
page.driver.browser.action.
move_to(find(:xpath, './/span[text()="Test"]')).
move_to(find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]')).
click(find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]')).
perform
对于那些隐藏的元素,预赋值将失败
elem1 = find(:xpath, './/span[text()="Test"]') #ok
elem2 = find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]') #ElementNotFound
page.driver.browser.action.
move_to(elem1).
move_to(elem2).
click(elem2).
perform
也许真正的问题是找到悬停 javascript 的可靠方法,它在某些页面上似乎隐藏得很好。
想法?
您可以触发 mouseenter 事件使其可见,然后使用选择器获取它。
首先您需要访问 selenium 驱动程序对象(可能是您的 page.driver
或 (page.driver.browser
)
<driver>.execute_script <<-JS
$("<some_selector>").trigger("mouseenter")
JS
请注意,这假设网页上的 jquery 是 运行。否则你可以用纯 js 重写它。
水豚hover
方法不适合你吗?
find(:xpath, './/span[text()="Test"]').hover
find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]').click
虽然我可能会重写类似
span = find(:xpath, './/span[text()="Test"]')
span.hover
span.find(:xpath, '../a[@title="Hidden Thing to click"]').click
目前,我经常将鼠标悬停在页面上的元素变为可见。
action builder 听起来很有前途,但似乎需要事先找到元素,而不是在操作过程中。
这行不通...
page.driver.browser.action.
move_to(find(:xpath, './/span[text()="Test"]')).
move_to(find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]')).
click(find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]')).
perform
对于那些隐藏的元素,预赋值将失败
elem1 = find(:xpath, './/span[text()="Test"]') #ok
elem2 = find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]') #ElementNotFound
page.driver.browser.action.
move_to(elem1).
move_to(elem2).
click(elem2).
perform
也许真正的问题是找到悬停 javascript 的可靠方法,它在某些页面上似乎隐藏得很好。
想法?
您可以触发 mouseenter 事件使其可见,然后使用选择器获取它。
首先您需要访问 selenium 驱动程序对象(可能是您的 page.driver
或 (page.driver.browser
)
<driver>.execute_script <<-JS
$("<some_selector>").trigger("mouseenter")
JS
请注意,这假设网页上的 jquery 是 运行。否则你可以用纯 js 重写它。
水豚hover
方法不适合你吗?
find(:xpath, './/span[text()="Test"]').hover
find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]').click
虽然我可能会重写类似
span = find(:xpath, './/span[text()="Test"]')
span.hover
span.find(:xpath, '../a[@title="Hidden Thing to click"]').click