单击一个按钮,弹出 window
Click a button that pops up a window
URL:
https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687
单击“添加”按钮会生成一个弹出窗口 window,您需要在其中输入您的凭据。我尝试了用 Selenium/Python 单击按钮以生成弹出窗口 window 的不同方法,但似乎没有任何效果。
我的代码片段:
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")))
看看这个: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()
要单击元素 添加,您可以使用以下任一方法 :
使用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 启用的元素,因此理想情况下,单击需要诱导 并且您可以使用以下任一项 :
使用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
浏览器快照:
URL: https://efun.toronto.ca/torontofun/Activities/ActivitiesCourseDetails.asp?aid=18830&cid=6455687
单击“添加”按钮会生成一个弹出窗口 window,您需要在其中输入您的凭据。我尝试了用 Selenium/Python 单击按钮以生成弹出窗口 window 的不同方法,但似乎没有任何效果。
我的代码片段:
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")))
看看这个: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()
要单击元素 添加,您可以使用以下任一方法
使用
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 启用的元素,因此理想情况下,单击需要诱导 并且您可以使用以下任一项
使用
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
浏览器快照: