Getting WebElement in text using XPath in a Selenium test

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

The WebElement I'm interested in finding

WebElement that interests me

Its HTML,

Custom CSS

Basically my WebElement, which I am trying to extract by text, contains an input element.

I am currently using

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

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

Even,

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

gave me no results. Although I am interested in the exact correspondence of the text.

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

Thanks!

+5
source share
1 answer

It seems the text is wrapped inside the label and not entered. try it

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

There is a good explanation of this xpath template here

+4
source

Source: https://habr.com/ru/post/1232748/


All Articles