How to use regular expressions in xpath in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can identify elements by matching its attributes partially with the help of regular expressions. In xpath, there are multiple methods to achieve this. They are listed below −

  • Using the contains() method. This means the string contains our given text.

Syntax

driver.find_element_by_xpath("//input[contains(@name,'sel')]")

It will search the input tag which contains the 'name' attribute containing 'sel' text.

  • Using the starts-with() method. This means the string starts with our given text.

Syntax

driver.find_element_by_xpath("//input[starts-with (@name,'Tut')]")

It will search the input tag which contains the 'name' attribute starting with 'Tut' text.

  • Using the ends-with() method. This means the string ends with our given text.

Syntax

driver.find_element_by_xpath("//input[ends-with (@name,'nium')]")

It will search the input tag which contains the 'name' attribute ending with 'nium' text.

Example

Code Implementation with contains()

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of contains() in xpath
driver.find_element_by_xpath("//input[contains(@id,'sc-i')]").
send_keys("Selenium")
#to close the browser
driver.close()

Code Implementation with starts-with()

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of starts-with() in xpath
driver.find_element_by_xpath("//input[starts-with(@id,'gsc')]").
send_keys("Selenium")
#to close the browser
driver.close()

Code Implementation with ends-with()

from selenium import webdriver
#browser exposes an executable file
#Through Selenium test we will invoke the executable file which will then #invoke actual browser
driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
# to maximize the browser window
driver.maximize_window()
#get method to launch the URL
driver.get("https://www.tutorialspoint.com/index.htm")
#to refresh the browser
driver.refresh()
# identifying the edit box with the help of ends-with() in xpath
driver.find_element_by_xpath("//input[ends-with(@id,'id1')]").
send_keys("Selenium")
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 10:57:10
Advertisements