When element doesn’t contain any id and name, using xpath to find the element is very important for writing selenium test. I list some sample locate for the element which doesn’t contain id or name.
1. get the link with the link text<a href=”link url”>Link Text</a> -> link=Link Text
2. get element with the element text
<a href=”link url”>Link Text</a> -> //a[text()='Link Text']
3. get element with part of the element text
<a href=”link url”>Link Text</a> -> //a[contains(text(), 'ink Tex')]
4. get element with an attribute
<a href=”link url”>Link Text</a> -> //a[@href='link url']
5. get element with two attributes
<input type=”text” value=”value”/> -> //input[@type='text' and @value='value']
XPather is a firefox plugin help you to find the xpath.
There is more element locators document to be found on Selenium Doc
February 12th, 2008 at 4:47 am
Nice! Thanks!
August 14th, 2008 at 12:01 pm
Is there a way to check for not equal? something like @type!=’text’ or @type’text’ ??? Not working for some reason
February 20th, 2009 at 8:57 pm
I have problem accessing input element using xpath if the value is set using javascript.
then set the value using javascript like
var testInput = document.getElementByID(’test’);
testInput.value = ‘testValue’;
then do xpath to get the input element(don’t use its id)
//input[@type='text' and @value='testValue'] will not give you the input element.
Need help.
February 20th, 2009 at 8:59 pm
the input element is truncated from my previous comment
input type=’text’ id=’test’ (angle braces removed)
February 20th, 2009 at 11:34 pm
Why do you want to use @value to locator the input field if you know the element id. It’s usually bad idea to locale input filed via the value. Usually, input fields contain id and name attribute. I recommend you use the id. The project I am working on is using label to locate the input filed.
locator is getAttribute(”//label[text()='Username']/@for”)
I hope that will help you solve the problem.
Regards!
May 26th, 2009 at 1:01 pm
XPather really helps. No need to invent these rules, just copy paste.