【问题标题】:Click a button that pops up a window单击一个弹出窗口的按钮
【发布时间】:2021-05-19 22:07:46
【问题描述】:

网址: 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")))

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:

    看看这个: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 语句是一种很好的做法。

    参考: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()
    
    

    【讨论】:

      【解决方案2】:

      要点击元素添加,您可以使用以下任一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 的元素,因此理想情况下,单击需要为element_to_be_clickable() 诱导WebDriverWait 的元素,您可以使用以下Locator Strategies 之一:

      • 使用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
        
      • 浏览器快照:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多