To find an element by text using Selenium WebDriver in Python, you can use the find_element_by_xpath method and an XPath expression matching the desired text. Here’s an example:
from selenium import webdriver
# Create a new instance of the Firefox driver
driver = webdriver.Firefox()
# Navigate to a webpage
driver.get("https://github.codetryout.com/")
# Find element by text using XPath
element = driver.find_element_by_xpath("//*[text()='Example Text']")
# Perform actions with the found element
element.click()
# Close the browser
driver.quit()
In the example above, find_element_by_xpath is used with the XPath expression “//*[text()=’Example Text’]”. This expression searches for any element that contains the exact text “Example Text”. You can modify the expression to match your specific text or adjust it to meet your needs.
Note that you need to have the Selenium WebDriver for Python (selenium) installed and have the appropriate WebDriver executable (e.g., geckodriver for Firefox) set up in your system’s PATH.