John Smith John Smith - 1 year ago 128
Python Question

Python Selenium

How do I use Python to simply find a link containing text/id/etc and then click that element?

My imports:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys

Code to go to my URL:

browser = webdriver.Firefox() # Get local session of firefox
browser.get(myURL) # Load page
#right here I want to look for element containing "Click Me" in the text
#and then click it

Answer Source

Look at method list of WebDriver class -

For example, to find element by its ID and click it, the code will look like this

element = browser.find_element_by_id("your_element_id")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download