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

6 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.

Leave a Reply