0

Currently, and often I have elements on a page that become visible when hovered.

action builder sounded promising, but seems to require elements found beforehand, not during an operation.

This won't work...

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

and pre-assignments will fail for those elements that are hidden

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

Maybe the real question is a sure fire way to find the onhover javascript, it seems well hidden on some pages.

Thoughts?

TangibleDream
  • 601
  • 8
  • 29

2 Answers2

1

Does Capybara hover method not work for you?

find(:xpath, './/span[text()="Test"]').hover
find(:xpath, ".//span[text()="Test"]]/../a[@title="Hidden Thing to click"]').click

although I'd probably rewrite that something like

span = find(:xpath, './/span[text()="Test"]')
span.hover
span.find(:xpath, '../a[@title="Hidden Thing to click"]').click
Thomas Walpole
  • 48,548
  • 5
  • 64
  • 78
0

You can trigger a mouseenter event which which will make it visible, then get it with a selector.

First you need access to a selenium driver object (maybe your page.driver or (page.driver.browser)

<driver>.execute_script <<-JS
  $("<some_selector>").trigger("mouseenter")
JS

note this assumes jquery is running on the webpage. You can rewrite it in plain js otherwise.

max pleaner
  • 26,189
  • 9
  • 66
  • 118
  • This threw a unknown error, is my syntax off ```we = find(:xpath, '//blah``` and ```execute_script ("$(#{we}).trigger(\"mouseenter\")")``` – TangibleDream Jan 21 '17 at 23:24
  • you haven't defined the `we` variable as something you can pass to javascript. The argument to the jquery selector is a string, so you'd interpolate it like `$('#{css_selector}')` see [javascript-use-xpath-in-jquery](http://stackoverflow.com/questions/12243661/javascript-use-xpath-in-jquery) – max pleaner Jan 21 '17 at 23:28
  • can i do an xpath selector instead? – TangibleDream Jan 21 '17 at 23:41
  • see [is-there-a-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver](http://stackoverflow.com/questions/10596417/is-there-a-way-to-get-element-by-xpath-using-javascript-in-selenium-webdriver) – max pleaner Jan 21 '17 at 23:42