How to Use XPath in watir to locate and operate on page elements

Source: Internet
Author: User
The last time I wrote an article on how to operate span elements in selenium, I mentioned how to use XPath to locate and manipulate page elements in selenium. I took a look at it yesterday. watir also provides support for xpath. If you are interested, you can check it out. Some reference documents and sample code are listed below.

The first thing to mention is a reference document. For the support for xpath in watir, click here to download. As mentioned above, the support for xpath must meet the following requirements:
1. The watir version is 1.5.x. This is easy to use. download and install the latest gem on the watir homepage;
2. For Versions later than rexml 3.1.3 installed, I use rexml 3.1.4. You can click here to download it.

For specific installation methods, see the watir FAQ (http://wiki.openqa.org/display/WTR/FAQ) and rexml install documentation. Of course, we also need some web development experience. At least we need to understand what XPath is. ^_^

After the installation and configuration are complete, you can use the following page as a test. The source code of the operated HTML page is as follows:

<! Doctype HTML public "-// W3C // dtd html Transitional // en">
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<HTML>
<Body>
<Div id = "Container">
<Div id = "entrypanel">
<Div class = "levelchooser" id = "entrypanel_levelchooser">
<SPAN class = "tab-panel">
<Div class = "stepchooser" id = "entrypanel_levelchooser_panel_stepchooser">
<Div class = "tabbedstepchooser" id = "entrypanel_levelchooser_panel_stepchooser_tabbedstepchooser">
<SPAN class = "tab-panel">
<Div id = "maincontent">
<Div id = "mainnav" style = "height: 228px">
<Div class = "leftarea" id = "entrypanel_levelchooser_panel_stepchooser_tabbedstepchooser_panel_proxylistpanel">
<Div class = "pnwebproxytreepanel">
<Div class = "proxytree">
<Span id = "entrypanel_levelchooser_panel_stepchooser_tabbedstepchooser_panel_proxylistpanel_proxytree">
<Div class = "wicket-Tree">
<Div class = "row" id = "inline">
<Div class = "A _">
<A id = "entrypanel_levelchooser_panel_stepchooser_tabbeds restart" onclick = "Var wcall = wicketajaxget ('/WebClient/client /? Wicket: interface =: 3: entrypanel: levelchooser: Panel: stepchooser: tabbedstepchooser: Panel: proxylistpanel: proxytree: I: 3: nodelink: prop & wicket: behaviorid = 0 ', function () {}, function () {}); return! Wcall; "href =" http: // Java-server/WebClient/client/launchcenter/entry/reservations # ">
<Span>
Main road Nijmegen
</Span> </a>
</Div>
</Div>
</Div>
</Span>
</Div>
</Div>
</Div>
</Div>
</Div>
</Span>
</Div>
</Div>
</Span>
</Div>
</Div>
</Div>
</Body>
</Html>

The watir code used for testing is as follows:

Require "watir"

Ie = watir: IE. New

Ie. Goto ("G: \ span.html ")

Ie. element_by_xpath ("// span [contains (text (), 'main road Nijmegen ')]"). Click

If the test code works properly and no error is reported, it indicates that watir has configured the support for xpath.

In addition, according to the watir official site, the current support for xpath does not include support for frame.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.