How to use text() in xpath in Selenium with python?

Selenium Web DriverAutomation TestingSoftware Testing

We can create a customized xpath with the help of the visible text on the page. This is achieved with the help of text() method in xpath.

The text() finds the object with the exact text match on the page.

Syntax

driver.find_element_by_xpath("//input[text()='Selenium']")

It will search for elements with visible text 'Selenium' on the page.

Example

Code Implementation with text().

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 text() in xpath
driver.find_element_by_xpath("//*[text()='GATE Exams']").click()
#to close the browser
driver.close()
raja
Published on 29-Jul-2020 10:58:49
Advertisements