Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding text on page with Selenium 2

How can I check whether a given text string is present on the current page using Selenium?

like image 847
thitemple Avatar asked May 12 '11 18:05

thitemple


People also ask

How do you getText from an element in Selenium?

We can get text from a webelement with Selenium webdriver. The getText() methods obtains the innerText of an element. It fetches the text of an element which is visible along with its sub elements. It ignores the trailing and leading spaces.

How do I find the partial text of an element?

We can find an element using the link text or the partial link text in Selenium webdriver. Both these locators can only be applied to elements with the anchor tag. The link text locator matches the text inside the anchor tag. The partial link text locator matches the text inside the anchor tag partially.

How do I get XPath to text?

So, inorder to find the Text all you need to do is: driver. findElement(By. xpath("//*[contains(text(),'the text you are searching for')]"));


1 Answers

The code is this:

def elem = driver.findElement(By.xpath("//*[contains(.,'search_text')]")); 
if (elem == null)  println("The text is not found on the page!");
like image 190
Grooveek Avatar answered Nov 15 '22 01:11

Grooveek