0

URL: https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687

Clicking the "Add" button generates a pop-up window where you need to enter your credentials. I've tried different ways of clicking the button with Selenium/Python to produce the popup window but nothing seems to works.

Snippet of my code:

from selenium import webdriver
import xpath
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

PATH = "C:\Program Files (x86)\chromedriver.exe"

driver = webdriver.Chrome(PATH)

driver.get('https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687')

add_buttom_try1 = WebDriverWait(driver,10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "Add"))).click()

add_buttom_try2 = WebDriverWait(driver,10).until(
    EC.presence_of_element_located((By.CLASS_NAME, "ui-state-active ui-corner-all link-button ajax-request from-full-page focus-child need-focus-pageobject")))
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Arty
  • 129
  • 8

2 Answers2

2

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()

enter image description here

Life is complex
  • 15,374
  • 5
  • 29
  • 58
0

To click on the element Add you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687")
    driver.find_element(By.CSS_SELECTOR, "a[title='Add To My Cart'] > span").click()
    
  • Using XPATH:

    driver.get("https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687")     
    driver.find_element(By.XPATH, "//span[text()='Add']").click()
    

The desired element is a AJAX enabled element, so ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get("https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[title='Add To My Cart'] > span"))).click()
    
  • Using XPATH:

    driver.get("https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Add']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

add

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352