Željko Filipin
Željko Filipin

Reputation: 57262

How to access element using Watir and XPath

I have some HTML that looks like this:

<h1 id="header">Header</h1>

I would like to click it using Watir and XPath.

Upvotes: 7

Views: 19570

Answers (6)

akostadinov
akostadinov

Reputation: 18594

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

Upvotes: 11

Antony Fuentes
Antony Fuentes

Reputation: 1103

Another example using xpath here:

browser.element xpath: "//div/cite[contains(.,'some text')]/ancestor::div[@class='rc']/h3/a"

Checkout this simple framework that I uploaded to Github: https://github.com/atfuentess/watir_cucumber_automation/

The stack used is: watir/cucumber/rspec

Perhaps it can help someone.

Upvotes: 0

Željko Filipin
Željko Filipin

Reputation: 57262

Also not XPath, but works:

browser.h1(:html, /header/).click

Upvotes: 1

Željko Filipin
Željko Filipin

Reputation: 57262

browser.h1(:xpath, "//h1[@id='header']").click

Upvotes: 5

Željko Filipin
Željko Filipin

Reputation: 57262

browser.element_by_xpath("//h1[@id='header']").click

Sources:

Upvotes: 5

Željko Filipin
Željko Filipin

Reputation: 57262

Not using XPath, but it works:

browser.h1(:id, "header").click

Upvotes: 0

Related Questions