Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get (text) in XPath

I have the following DOM structure / HTML, I want to get (just practicing...) the marked data. enter image description here

The one that is under the h2 element. that div[@class="coordsAgence"] element, has some more div children below and some more h2's.. so doing:

div[@class="coordsAgence"]

Will get that value, but with additional unneeded text. UPDATE: The value (From this example) that I basically want is that: "GALLIER Dennis" text.

like image 286
snoofkin Avatar asked Mar 28 '11 00:03

snoofkin


People also ask

How do I get text in XPath?

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

What is text () in XPath?

XPath text() function is a built-in function of the Selenium web driver that locates items based on their text. It aids in the identification of certain text elements as well as the location of those components within a set of text nodes. The elements that need to be found should be in string format.

How do I get text in Selenium?

We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).

Why * is used in XPath?

The '*' selects all element nodes descending from this current node with the @id -attribute-value equal to 'Passwd'.


1 Answers

It seems you want the first text node in that div:

div[@class="coordsAgence"]/text()[1]

should do it.

Note that this assumes that there is actually no whitespace between those comments inside <div class="coordsAgence">; otherwise that whitespace will constitute additional text nodes that you'll have to account for.

like image 110
Liza Daly Avatar answered Sep 18 '22 20:09

Liza Daly