How to access element after another element using Watir?
-
16-09-2019 - |
Question
I have a page with HTML like this:
<a>text</a>
<img src="image.png" />
<a>text</a>
I would like to click the second link. Links have completely identical attributes. The only thing that is different is that the second link is after an image.
This always clicks the first link:
browser.link(:text, "text").click
Solution
browser.link(:after?, browser.image(:src, /image/)).click
Sources:
- http://wiki.openqa.org/display/WTR/How+and+What
- http://wiki.openqa.org/display/WTR/Ways+Available+To+Identify+HTML+Tag
- http://wiki.openqa.org/display/WTR/Methods+Supported+by+Element
- http://www.mail-archive.com/wtr-general%40rubyforge.org/msg08829.html
- http://zeljkofilipin.com/2007/05/25/watir-has-a-new-method-after/
OTHER TIPS
You can also do:
browser.link(:text=>"text",:index=>2)
In general, you can include any combination of conditions via a ruby hash.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow