Finding Elements by XPath

XPath is just a really effective vocabulary to state which component to recognize. If you are using it properly, it may create really dependable and low-maintenance locators, but when you utilize it improperly, it may produce really fragile check instances.

Let’s observe a few examples:

<table name=”cart”>
<tr id=”item1″>
<td class=”name item”>Mp3 Download: foobar.mp3</td>
<td class=”name item”><input name=”qty” class=”name item formfield disabled” /></td>
</tr>
<tr id=”item2″>
<td class=”name item”>Mp3 Player</td>
<td class=”name item”><input id=”item2_quantity” name=”qty” class=”name item formfield required” type=”text” /></td>
</tr>

</table>

You are able to goal the outlined enter area utilizing the subsequent XPath expression:

//table/tr[2]/td/input
//input[@id=’item2_quantity’]
(//table[@name=’cart’]//input)[2]
//input[contains(@class,’required’)]
//input[contains(@class,’required’) and type=’text’]

As well as for the final choice, your espresso phone might seem like:

driver.findElement(By.xpath("//input[contains(@class='required') and type='text']"));

While you may speculate, several of those expression won’t be as dependable as others. Of those //desk/tr[2]/td/enter may be the toughest since it might break-even using smallest customization towards the webpage framework. It may take sometime to understand XPath in the event that you aren’t acquainted with it, but it’s worth the full time in the event that you intend to invest lots of period composing UI automatic assessments. Regardless don’t depend on resources, including selenium IDE, to create the best xpath expression for you personally. They are able to help you to get started-but they’re often poor at pinpointing the more dependable XPaths.

XPath namespaces

There are some special cases you should be aware of when you work with XPath, like when you are trying to interact with SVG. Like in the example here, the html looks something like this:
view source
print
?
<div id=”svgchart”>

<svg xmlns=”http://www.w3.org/2000/svg”>
<g>
<path …/>
</g>

</svg>

</div>

There are several unique instances you ought to be conscious of whenever you use XPath, like when you’re attempting to connect to SVG. Like within the instance below, the html appears something similar to this:

Below, if you are using XPath like:

//svg

You’ll get MISTAKE org.openqa.selenium.NoSuchElementException: Not Able To find component. The reason being the svg component is in another namespace. You’ll need certainly to stipulate your xpath using the namespace uri such as this alternatively:


//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']

XPath performance

Therefore, if XPath’s are so flexible, why doesn’t everybody choose these? It’s since they’re usually the slowest, particularly in mature variations of IE! There are several methods you may make XPath’s quicker, however they nevertheless really are a several occasions slower-than ids or titles.

Finding Elements by CSS

Flash locators may be used to recognize a significant number of components on the webpage. Let’s understand this html little for example:

<table name=”cart”>
<tr id=”item1″>
<td class=”label”>Mp3 Download: foobar.mp3</td>
<td class=”item”><input name=”qty” class=”formfield disabled” /></td>
</tr>
<tr id=”item2″>
<td class=”label”>Mp3 Player</td>
<td class=”item”><input id=”item2_quantity” name=”qty” class=”formfield required” type=”text” /></td>
</tr>

</table>

In this instance the flash locator versions feasible for the outlined enter area are:

input.required
input[class~='required']
input.required[type='text']
#item2_quantity
input#item2_quantity

The flash locator may possibly not be as significant as XPath, however it usually completes quicker.

Leave a Reply

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