Jonas Byström
Jonas Byström

Reputation: 26129

Selenium: how to find div with specific content?

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.

Upvotes: 7

Views: 18296

Answers (3)

edA-qa mort-ora-y
edA-qa mort-ora-y

Reputation: 31851

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()

Upvotes: 1

Dave Hunt
Dave Hunt

Reputation: 8223

You could also use CSS locators:

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

Could be located using:

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

Upvotes: 8

krosenvold
krosenvold

Reputation: 77101

try this:

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

Upvotes: 7

Related Questions