Implement Java Interface in Ruby Selenium DSL I
Jan 07

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

12 Responses to “Selenium Element Locators”

  1. Michael Says:

    Nice! Thanks!

  2. protector Says:

    Is there a way to check for not equal? something like @type!=’text’ or @type’text’ ??? Not working for some reason

  3. Ash Says:

    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.

  4. Ash Says:

    the input element is truncated from my previous comment

    input type=’text’ id=’test’ (angle braces removed)

  5. admin Says:

    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!

  6. lang Says:

    XPather really helps. No need to invent these rules, just copy paste.

  7. Arthur Says:

    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?

  8. Arthur Says:

    All good now, this is working
    verifyElementPresent
    //div[@id='PortfolioButton']/span[contains(@class,'yui-button-disabled ')]

  9. Felipe Silva Says:

    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 

      

  10. Felipe Silva Says:

    <


    <
    <
    <
    <
    <ABC
    <

    <
    <
    <
     $19.99 
    <
    <
      
    <
    <

  11. Felipe Silva Says:

    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!

  12. Guru Says:

    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

Leave a Reply