How to access element using Watir and XPath
18,800
Solution 1
browser.h1(:xpath, "//h1[@id='header']").click
Solution 2
After watir-webdriver 0.5.1 selecting random element with an xpath was updated to:
browser.element(:xpath => "//h1[@id='header']").click
thanks to: https://groups.google.com/forum/#!topic/watir-general/c6Orvy7Qalw
Solution 3
browser.element_by_xpath("//h1[@id='header']").click
Sources:
Solution 4
Also not XPath, but works:
browser.h1(:html, /header/).click
Author by
Željko Filipin
Freelance software engineer. International contractor at the Wikimedia Foundation.
Updated on June 11, 2022Comments
-
Željko Filipin almost 2 years
I have some HTML that looks like this:
<h1 id="header">Header</h1>
I would like to click it using Watir and XPath.
-
Dan Herman almost 12 yearsthis doesn't work for me. There does not appear to be element_by_xpath anymore
-
Željko Filipin almost 12 yearsI think element_by_xpath was deprecated recently.
-
Željko Filipin almost 12 yearsI have just checked,element_by_xpath was removed from watir-webdriver in version 0.5.1: github.com/watir/watir-webdriver/blob/master/CHANGES.md#051
-
akostadinov about 10 yearsSeems like it is replaced with
.element(:xpath => "str")
, have not tested it yet. Courtesy of groups.google.com/forum/#!topic/watir-general/c6Orvy7Qalw -
akostadinov about 10 yearsI assume
:html
means the html source of the element, correct? So in your example matching it with a pattern?