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.
February 11th, 2010 at 8:11 pm
Could you guys help with this?
Disable State
div id=”PortfolioButton”>
span id=”yui-gen2″ class=”yui-button yui-menu-button yui-button-disabled yui-menu-button-disabled”>
Enable State
div id=”PortfolioButton”>
span id=”yui-gen2″ class=”yui-button yui-menu-button”>
Given the above to determine the state, how do I verified that the button is disabled?
February 11th, 2010 at 9:17 pm
All good now, this is working
verifyElementPresent
//div[@id='PortfolioButton']/span[contains(@class,'yui-button-disabled ')]
February 18th, 2010 at 6:13 pm
Hi All,
I have a problem, my system under test is a data driven system, and I need to find one radio-button with specific label in one list of radio-buttons, follow my HTML from one radio button with its label “ABC”, I need to click on radio-button which have the label “ABC”:
ABC
$19.99
February 18th, 2010 at 6:15 pm
<
<
<
<
<
<
<
<ABC
<
<
<
<
<
$19.99
<
<
<
<
February 18th, 2010 at 6:24 pm
Sorry, but I can’t post the HTML here, please who will help me send me an email to dexfelipe.88@gmail.com asking me for the HTML that I send it.
Thanks!
March 7th, 2010 at 1:50 pm
Good!
Its nice that you gave these functionality…
But can you pls help me in putting this in Java..
i. I need to acquire the id of a particular element by giving the text that contains.
ii. same as above but to acquire the table id.. Table.m.n of a mentioned text. m.n is the locator.. row.col
Thanks