网址: https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687
单击“添加”按钮会生成一个弹出窗口,您需要在其中输入凭据。我尝试了不同的方法来使用 Selenium/Python 单击按钮来生成弹出窗口,但似乎没有任何效果。
我的代码片段:
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")))
答案 0 :(得分:2)
看看这个:strategies to locate elements on a page
以下是找到“添加”按钮的一些方法:
使用 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()
使用 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()
使用 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()
有时您的代码可以在“按钮”可点击之前执行。发生这种情况时会抛出错误。在查找可点击的元素时,添加 WebDriverWait 语句是一个很好的做法。
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()
答案 1 :(得分:0)
要单击元素 Add,您可以使用以下任一 Locator Strategies:
使用 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()
使用 XPATH
:
driver.get("https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687")
driver.find_element(By.XPATH, "//span[text()='Add']").click()
所需元素是启用 AJAX 的元素,因此理想情况下,要单击需要为 WebDriverWait 引入 element_to_be_clickable()
的元素,您可以使用以下任一 {{ 3}}:
使用 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()
使用 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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照: