Home > Enterprise >  Xpath: How to use wildcard or regex to match text()?
Xpath: How to use wildcard or regex to match text()?

Time:04-04

<span> GDP, By Country, 2015 As Base Year </span>

<span> GDP, By City, 2015 As Base Year </span>

<span> GDP, 2015 As Base Year</span>

Other span tags starting with "GDP" and ending with "2015 As Base Year" 

I want to match "GDP, 2015 As Base Year"

I can use

//span[text()='GDP, 2015 As Base Year']

But 2015 could change in the future.

I am using Selenium with Chrome and "find_element_by_xpath". So how do I use wildcard or regex to construct a more robust selector?

I tried
//span[matches(text(), 'GDP, \d As Base Year')]

But get error:
The string '//span[matches(text(), 'GDP, \d As Base Year')]' is not a valid XPath expression.

CodePudding user response:

matches() is xpath 2 function, Selenium supports xpath 1. You could however use something like

//span[boolean(number(substring-before(substring-after(., "GDP, "), "As Base Year")))]

substring-after(., "GDP, ") will return a partial text after "GDP, ", "2015 As Base Year"

substring-before("2015 As Base Year", "As Base Year") will return "2015 "

number() will return an int if the text can be converted to number, else NaN

boolean() will check if the converted text has value and it's not `NaN.

CodePudding user response:

A easier approach would be to include both the substring GDP and As Base Year as follows:

  • Using starts-with() and contains():

    //span[starts-with(., 'GDP') and contains(., 'As Base Year')]
    
  • Using contains() twice:

    //span[contains(., 'GDP') and contains(., 'As Base Year')]
    
  • Simply using the text As Base Year:

    //span[contains(., 'As Base Year')]
    
  • Related