Selenium: how to find div with specific content?

17,670

Solution 1

try this:

 //div[@class='gwt-Label' and contains(., 'Logout')]

Solution 2

You could also use CSS locators:

<div class="gwt-Label">This FindMe DIV</div>

Could be located using:

css=.gwt-Label:contains('FindMe')

Solution 3

Perhaps your XPath just isn't quite doing what you think. You may need to use the string() function to concatenate all the text in a block.

For example, in TestPlan (using Selenium as backend) you would do something like this:

Click //div[@class='gwt-Label'][contains(string(),'Logout')]

Note the use of string()

Share:
17,670
Mars
Author by

Mars

Python haxxorz and parapsychology student. Creator of the eminent finplot library: https://github.com/highfestiva/finplot. Once upon a time made a UFO short documentary in Swedish: https://www.youtube.com/watch?v=HFF528pc3O8. Constantly at a fork in the road.

Updated on June 16, 2022

Comments

  • Mars
    Mars about 2 years

    I need to find a <div> with certain content and click it from Selenium, as so:

    <tr>
      <td>clickAndWait</td>
      <td>//div[@class='gwt-Label' ***WITH CONTENT='Logout'***]</td>
      <td>5000</td>
    </tr>
    

    Is there some way to do this? I don't want to use an absolute xpath.