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
Share:
18,800
Željko Filipin
Author by

Željko Filipin

Freelance software engineer. International contractor at the Wikimedia Foundation.

Updated on June 11, 2022

Comments

  • Željko Filipin
    Ž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
    Dan Herman almost 12 years
    this doesn't work for me. There does not appear to be element_by_xpath anymore
  • Željko Filipin
    Željko Filipin almost 12 years
    I think element_by_xpath was deprecated recently.
  • Željko Filipin
    Željko Filipin almost 12 years
    I 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
    akostadinov about 10 years
    Seems like it is replaced with .element(:xpath => "str"), have not tested it yet. Courtesy of groups.google.com/forum/#!topic/watir-general/c6Orvy7Qalw
  • akostadinov
    akostadinov about 10 years
    I assume :html means the html source of the element, correct? So in your example matching it with a pattern?