How to access element using Watir and XPath

孤街醉人 提交于 2019-12-12 08:44:18

问题


I have some HTML that looks like this:

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

I would like to click it using Watir and XPath.


回答1:


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




回答2:


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

Sources:

  • http://wiki.openqa.org/display/WTR/XPath
  • http://zeljkofilipin.com/2007/07/03/find-element-by-xpath/



回答3:


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



回答4:


Also not XPath, but works:

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



回答5:


Not using XPath, but it works:

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



回答6:


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.



来源:https://stackoverflow.com/questions/1476457/how-to-access-element-using-watir-and-xpath

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!