In my webpage, there's a div
with a class
named Test
.
How can I find it with XPath
?
By adding square brackets with index. By using position () method in xpath.
Go to the First name tab and right click >> Inspect. On inspecting the web element, it will show an input tag and attributes like class and id. Use the id and these attributes to construct XPath which, in turn, will locate the first name field.
To identify the element with css, the expression should be tagname[class='value'] and the method to be used is By. cssSelector. To identify the element with xpath, the expression should be //tagname[@class='value']. Then, we have to use the method By.
This selector should work but will be more efficient if you replace it with your suited markup:
//*[contains(@class, 'Test')]
Or, since we know the sought element is a div
:
//div[contains(@class, 'Test')]
But since this will also match cases like class="Testvalue"
or class="newTest"
, @Tomalak's version provided in the comments is better:
//div[contains(concat(' ', @class, ' '), ' Test ')]
If you wished to be really certain that it will match correctly, you could also use the normalize-space function to clean up stray whitespace characters around the class name (as mentioned by @Terry):
//div[contains(concat(' ', normalize-space(@class), ' '), ' Test ')]
Note that in all these versions, the * should best be replaced by whatever element name you actually wish to match, unless you wish to search each and every element in the document for the given condition.
Most easy way..
//div[@class="Test"]
Assuming you want to find <div class="Test">
as described.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With