How do I find an element that contains specific text in Selenium Webdriver (Java)?

How do I find an element that contains specific text in Selenium Webdriver (Java)?

To locate an element, you need to find the element based on its XPath as you can specify the text the element should contain using "contains(text(), 'YOUR TEXT HERE')".

WebElement element = driver.findElement(By.xpath("//*[contains(text(), 'Wikipedia')]"));

To locate the element based on whether any of its children contain the correct text use "contains(., 'YOUR TEXT HERE')" instead.

WebElement element2 = driver.findElement(By.xpath("//*[contains(., 'Wikipedia')]"));

Leave a reply

Your email address will not be published. Required fields are marked*

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
7 + 4 =

Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.