Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we using regular expression in xpath value?

Can we using regular expression in xpath value? I am using xpath value to identify an element on web for automation.

I have following :xpath value.

:xpath,'//*[@id="ngdialog4"]/div[2]/div[2]/table/tbody/tr/td[1]/input'

But, the last digit 4 ngdialog4 is not constant and it keeps on changing each time i open pop-up... can i use some regular expression to match any digit?

like image 550
RayM Avatar asked Jan 10 '15 18:01

RayM


People also ask

Can I use regular expression in XPath?

XPath does not support regular expression. XPath supports various functions like starts-with() or contains() but it does not support regular expression.

How do I use regular expressions in selenium locators?

We can use regex in locators in Selenium webdriver. This can be achieved while we identify elements with the help of xpath or css locator. Let us have a look at the class of an element in its html code. The class attribute value is gsc-input.

Where Can regular expressions be used?

Regular expressions are used in search engines, in search and replace dialogs of word processors and text editors, in text processing utilities such as sed and AWK, and in lexical analysis.

Can we use and in XPath?

Using OR & AND In the below XPath expression, it identifies the elements whose single or both conditions are true. Highlight both elements as 'First Name' element having attribute 'id' and 'Last Name' element having attribute 'name'. In AND expression, two conditions are used.


1 Answers

You could have theoretically used matches(), but it is a part of xpath 2.0, which webdriver doesn't support, see a detailed explanation here:

  • Is string matches() supported in Selenium Webdriver 2?

Apply a starts-with() check instead:

//*[starts-with(@id, "ngdialog")]
like image 67
alecxe Avatar answered Sep 16 '22 21:09

alecxe