Take a look at this: strategies to locate elements on a page
Here are some ways to find the "Add" button:
Using XPATH:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687')
add_button = driver.find_elements_by_xpath("//a[contains(@href,'Add')]")
add_button[0].click()
Using CSS_SELECTOR:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687')
add_button = driver.find_elements_by_css_selector('.ui-state-active')
add_button[2].click()
USING LINK_TEXT:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687')
add_button = driver.find_element_by_link_text('Add')
add_button.click()
Sometimes your code can execute before the "button" is clickable. When this happens an error will be thrown. Adding a WebDriverWait statement is a good practice when looking for an element that is clickable.
ref: selenium wait statements
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_options = Options()
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
# disable the banner "Chrome is being controlled by automated test software"
chrome_options.add_experimental_option("useAutomationExtension", False)
chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
driver = webdriver.Chrome('/usr/local/bin/chromedriver', options=chrome_options)
driver.get('https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687')
# one method
wait = WebDriverWait(driver, 30)
add_button = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Add")))
add_button.click()
# another method
# wait = WebDriverWait(driver, 30)
# wait.until(EC.element_to_be_clickable((By.LINK_TEXT, "Add"))).click()
