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]')]
Sign up to request clarification or add additional context in comments.

2 Comments

is there a way to add the classname or any additional parameter to the xpath selector above?
@Eliyahu Yes. you can easily add //div[contains(text(),'[email protected]')][@class='classname']. Using @ since classname is an attribute
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]']

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.