user3621889
user3621889

Reputation: 1

How to find an element by its inner text with webdriver

I'm trying to find and select an element by it's inner text. My script navigates to an inbox where I need to select a specify email. All of the emails in the inbox have the same ids and class where they differ is the inner text of the class.

I have tried the following and other variations with no success.

driver.findElement(By.xpath("//div[text()=\\"REMINDER: Your Password Expires in 2 days\\"]"));

HTML:

<div id="divSubject" class="c3 ur">REMINDER: Your Password Expires in 2 days</div>
<div id="divSubject" class="c3 ur">REMINDER: Your Password Expires in 3 days</div>
<div id="divSubject" class="c3 ur">REMINDER: Your Password Expires in 4 days</div>

Upvotes: 0

Views: 3554

Answers (1)

Richard
Richard

Reputation: 9019

The following should work:

driver.findElement(By.xpath("//div[contains(text(),'REMINDER: Your Password Expires in 2 days')]"));

Also, your problem may have been with the escaping of the ". This should also, in theory, work:

driver.findElement(By.xpath("//div[text()='REMINDER: Your Password Expires in 2 days']"));

I tested both selectors in Chrome, both appear to work.

Upvotes: 1

Related Questions