Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use XPath contains() for specific text?

Say we have an HTML table which basically looks like this:

2|1|28|9|
3|8|5|10|
18|9|8|0|

I want to select the cells which contain only 8 and nothing else, that is, only 2nd cell of row2 and 3rd cell of row3.

This is what I tried: //table//td[contains(.,'8')]. It gives me all cells which contain 8. So, I get unwanted values 28 and 18 as well.

How do I fix this?

EDIT: Here is a sample table if you want to try your xpath. Use the calendar on the left side-https://sfbay.craigslist.org/sfc/

like image 932
MasterJoe Avatar asked Sep 22 '16 22:09

MasterJoe


People also ask

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 find XPath text contains?

The syntax for locating elements through XPath- Using contains() method can be written as: //<HTML tag>[contains(@attribute_name,'attribute_value')]

What is text () function 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 use attribute for contain in XPath?

Using the XPath contains() function, we can extract all the elements on the page that match the provided text value. Here, tag: tag is the name of the tag that contains the specific word. word: In this case, the word refers to the text that must be discovered in a specific string.


2 Answers

Be careful of the contains() function.

It is a common mistake to use it to test if an element contains a value. What it really does is test if a string contains a substring. So, td[contains(.,'8')] takes the string value of td (.) and tests if it contains any '8' substrings. This might be what you want, but often it is not.

This XPath,

//td[.='8']

will select all td elements whose string-value equals 8.

Alternatively, this XPath,

//td[normalize-space()='8']

will select all td elements whose normalize-space() string-value equals 8. (The normalize-space() XPath function strips leading and trailing whitespace and replaces sequences of whitespace characters with a single space.)

Notes:

  • Both will work even if the 8 is inside of another element such as a a, b, span, div, etc.
  • Both will not match <td>gr8t</td>, <td>123456789</td>, etc.
  • Using normalize-space() will ignore leading or trailing whitespace surrounding the 8.
like image 129
kjhughes Avatar answered Oct 12 '22 23:10

kjhughes


Try the following xpath, which will select the whole text contents rather than partial matches:

//table//td[text()='8']

Edit: Your example HTML has a tags inside the td elements, so the following will work:

//table//td/a[text()="8"]

See example in php here: https://3v4l.org/56SBn

like image 31
jedifans Avatar answered Oct 12 '22 22:10

jedifans