Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting WebElement by Text using XPath in Selenium test

I would like to find any WebElement based on text using XPath.

WebElement that I am interested to find,

WebElement that I am interested in

Its HTML,

Its CSS

Basically my WebElement that I am trying to retrieve by Text contains an input element.

I currently use,

driver.findElement(By.xpath("//*[normalize-space(text()) = 'Own Hotel']"));

which does not find the WebElement above, but it usually works to retrieve all other web elements.

Even,

By.xpath("//*[contains(text(),'Own Hotel')]")

did not give me any results. Although I am interested in exact text match.

I am looking for a way to find web element by text immaterial of the elements that are present inside the web element. If text matches, it should return the WebElement.

Thanks!

like image 568
LINGS Avatar asked Oct 01 '15 20:10

LINGS


People also ask

How do I get the text of an element using XPath?

WebDriver driver; String getText = driver. findElement(By. xpath("your xpath")). getText();

Can we use text () in XPath?

Locating Strategies- (By XPath- Using text()) In this section, you will learn how to locate a particular web element by XPath- Using text() method. "text() method" is used to identify an element based on the text available on the web page.

How do you getText from an element in Selenium?

getText() Method in Selenium This method helps retrieve the text, which is basically the innertext of a WebElement. getText() method returns string as a result. It removes the whitespaces if present in the front and back of the string.


2 Answers

It seems text is wrapped inside a label and not input. Try this

driver.findElement(By.xpath(".//label[text()[normalize-space() = 'Own Hotel']]"));

There is nice explanation about this xpath pattern here

like image 195
nilesh Avatar answered Sep 22 '22 01:09

nilesh


In the HTML below:

HTML

The innerText Own Hotel within the <input> node contains a lot of white-space characters in the beginning as well at the end. Due to the presence of these leading and trailing white-space characters you can't use the location path text() as:

text() selects all text node children of the context node


As an alternative, you need to use the String Function string normalize-space(string?) as follows:

driver.findElement(By.xpath("//*[normalize-space()='Own Hotel']"));

However, it would a better idea to make your search a bit more granular adding the tagName and preferably an unque attribute as follows:

  • Using tagName and normalize-space():

    driver.findElement(By.xpath("//input[normalize-space()='Own Hotel']"));
    
  • Using tagName, and normalize-space():

    driver.findElement(By.xpath("//input[@name='ownHotel' and normalize-space()='Own Hotel']"));
    

References

you can find a couple of relevant discussions using normalize-space() in:

  • How to click on a link with trailing white-space characters on a web page using Selenium?
  • How to locate and click the element when the innerText contains leading and trailing white-space characters using Selenium and Python
  • How to click on the button when the textContext contains leading and trailing white-space characters using Selenium and Python
like image 34
undetected Selenium Avatar answered Sep 21 '22 01:09

undetected Selenium