John Smith John Smith - 5 months ago 12
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

Look at method list of WebDriver class - http://selenium.googlecode.com/svn/trunk/docs/api/py/webdriver_remote/selenium.webdriver.remote.webdriver.html

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")
element.click()