Time for action – finding elements by direct XPath - Selenium

Instead of using the //, you can use a single / but you will need to make sure that the first node in your query is HTML. Let's see an example of this:

  1. Open Selenium IDE.

  2. Navigate to http ://www.wisdomjobs.com/chapter(Locators).

  3. Type xpath=/html/body/div[2]/div[3]/input into the Target input of Selenium IDE.

  4. Click on the Find button.

What just happened?

The previous locator will have found the same element as before. This type of XPath query will find the element fractionally quicker but if your UI is going to change, it may fail if the element is moved into a different area of the page. One thing to really note is that XPath locators can be extremely fragile. They can find what you want but the slightest change to your HTML and they break, meaning that you need to do maintenance on that test. I would recommend only using these if you have to.

You will have noticed that parent and child nodes are in the same query. Since HTML has a tree structure, it just notifies the query that it needs to start at the html node, then move to its child node, body, then to body's child, and so on until it reaches the end of the query. Once it has done that it will stop executing the query.

Using XPath to find the nth element of a type

There are a lot of occasions where as a Selenium user you will have to click on an edit button in a table so that you can update something specific. Have a look at the button that you wish to click; it does not have a unique name or ID. An example of this is the button with the value "Sibling Button".

When doing a query against the DOM, an array of elements is returned to Selenium that match the query. For example if you were to do //div on the Chapter 2 page of the website, there are three elements returned to Selenium. If your test is only relying on the first item in your test, then it will try and access only the first item. If you wanted to interact with the second element then your query would look like //div[2]. Note that the second to nth element need to be sibling nodes of the first element that is returned. If they are not and you were to access the element it would fail saying that it could not find them.

We can see this with the input buttons that are present on the page. They all reside in their own containing div element, so do not have any sibling elements that are also input elements. If you were to put //input[2] into Selenium IDE, it would not be able to find the element and fail.

You can see an example of this in the following screenshot:

Using element attributes in XPath queries

There are times that you will need to find elements that are the same except for the difference in one or two attributes. To handle this we can add the attributes to the query so that we can try to make the element more unique for use in the test. The format can be used for any attribute on any element. It will always follow :

xpath=//element[@ attribute='attribute value'].

For example, if you have two div elements on the page, but they only differ by the class attribute,

your XPath query would look like the following: xpath=//div[@class='classname'].

Try doing this with Selenium yourself by trying to identify something unique about the div elements on the page. When you have completed the task your query should look like one of the following in the next screenshot:

Doing a partial match on attribute content

As mentioned earlier there are times where there is no way for a developer to create a static ID for elements on the page. This could be down to the fact that the element is being loaded asynchronously via AJAX or because it is using the key of the data as it is stored in the database.

There are times where only part of the ID is dynamic. This is to allow the developer to cram more information onto the page so that the user has everything they need. We will need to have a mechanism to work with these elements.

To do the partial match, your query will need to have the word contains with the attribute and the partial match that it needs. For example, if you wanted to access the element that has the text in it "This element has an ID that changes every time the page is loaded", you will use //div[contains(@id,'time_')].

This is due to the first part of the ID always being static. The locator could also use starts-with instead of contains to make the XPath query stricter in what is returned. The queries in the following screenshot will find the same element on the page:

Finding an element by the text it contains

Finding elements by the text they contain can be quite useful when working with web pages that have been created dynamically. The elements could be from using a web based WYSIWYG editor or you might just like to find a paragraph on the page with specific text to then do further queries on.

To do this your query will need to have the text() method call in the query.

It will match the entire contents of the node if it has the format //element[text() = 'inner text'].

As seen in the previous section, your query can use the contains keyword to allow it to have a bit more leniency to what it finds. Next you can find a screenshot of queries that will find the same element as the previous section:

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Selenium Topics