«

»

Apr 15

Selenium RC Cookbook, Part 1

(This cookbook assumes you know the basics about how to get started with Selenium RC. These examples are also in Java, but should be pretty much consistent for any language)

Finding text in a certain place on the page:

Use XPath, treating the HTML of your page as well-formed XML.

browser.getText("//div[@id='content']/table/*/tr[2]/td[1]")

(In English: at any level of depth, find the div where the id=”content”. Then find the table element under that, and then give us the text of the first cell of the second row.)

Finding text anywhere on the page

browser.isTextPresent("Your Text Here");

Get the text of a URL link

Assuming you have text like: <a href=”…” > your link text here </a>, you could consider:

browser.getText("//a[@href='...']");

Note the consistency with the other example above – anytime you have an opening and closing <element> </element> tag, you can use getText() and XPath to find it.

Get the href of a URL link

Again, assuming you have text like: <a href=”my_link_here” > your link text here </a>, you could consider:

browser.getAttribute("//a[1]/@href");

which would return: my_link_here

Note the slight differences with getText() above, but the idea is generally the same.

How do I find what’s in my Select menu?

String[] options = browser.getSelectOptions("//form//select");

This would return an array of the option text from the only <select> element inside the only <form> element.

If you had more than one form, or more than one select:

String[] options = browser.getSelectOptions("//form[1]//select[1]");

This would return the options listed in the first select of the first form on the page.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>