chris
chris

Reputation: 29

Can't find exact text in selenium

I have a problem here where I need to search for an exact text using selenium. I would like to search for exact text with value "SER20170905111755" but in actual fact, there is another similar text with value "SER20170905111755 - AF-CR-613" on the page also.

I have tried using this

String text = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[contains(., 'SER20170905104010')]"))).getText();

it returns "SER20170905111755 - AF-CR-613" to me. Then i tried with

String text = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[text()='SER20170905104010']"))).getText();

it throws TimeOut exception. Seems it can't locate the text. But, if I replace it with this

String text = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='SER20170905104010']"))).getText();

it returns the expected value I need, which is "SER20170905111755". Below is the HTML code of the page for both texts

  1. SER20170905111755 - AF-CR-613 enter image description here

  2. SER20170905111755 enter image description here

Can you please help how can I perform exact text search by using //* instead of //div?

Upvotes: 0

Views: 192

Answers (2)

iamsankalp89
iamsankalp89

Reputation: 4739

Try this xapths

//div[@class='tabHeader']

or

//div[@class='tabHeader'][contains(text(),'SER20170905111755')]

Upvotes: 1

Auro Sarma
Auro Sarma

Reputation: 441

You can try using not predicate with your xpath as below

By.xpath("//*[contains(., 'SER20170905104010') and not(contains(.,'SER20170905111755 - AF-CR-613'))]")

I am also not sure about this but just have a try and let me know.

Upvotes: 0

Related Questions