13

I need to find the following element on the web page

<div class="b-datalist__item__addr">[email protected]</div>

I'm coding with Java for Selenium WebDriver.
Need the exact CSS selector for this element to use it with driver.findElement(By.cssSelector(the-selector).click command.
div[class='b-datalist__item__addr'] selector is not good enough since I must search according to [email protected] text that is not a link so I can't use findElement(By.linkText()) command.

1

2 Answers 2

16

Css does not allow you do text based search. xpath is the only option there.

//div[contains(text(),'[email protected]')]
2
  • is there a way to add the classname or any additional parameter to the xpath selector above?
    – Prophet
    Jun 6, 2015 at 20:15
  • 1
    @Eliyahu Yes. you can easily add //div[contains(text(),'[email protected]')][@class='classname']. Using @ since classname is an attribute
    – Saifur
    Jun 6, 2015 at 20:32
3

contains(node, substring) might solve the problem, but note that if there are several elements with similar text contents, e.g.

Predicate [contains(text(),'[email protected]')] will match all of them

In this case it's better to use starts-with(node, substring):

//div[starts-with(text(),'[email protected]')]

or fetch required element by exact text content:

//div[text()='[email protected]']
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.