【问题标题】:Not able to access dropdown menu using Selenium with Python无法使用 Selenium 和 Python 访问下拉菜单
【发布时间】:2022-01-12 05:56:49
【问题描述】:

我正在尝试使用 Selenium 编写 Python 脚本,以查看是否可以自动执行我创建的示例事件的注册过程。这是活动页面:https://www.tickettailor.com/events/testing4/621753

我可以访问该页面并点击“加入客人名单”按钮:

from selenium import webdriver
driver = webdriver.Chrome('path_to_chromedriver')
driver.get('https://www.tickettailor.com/events/testing4/621753')
button = driver.find_element_by_link_text('Join the guestlist')
button.click()

之后,我需要从 Category1 票的下拉菜单中选择“1”,让 Category2 中的票数保持为 0。之后,我需要输入姓名和电子邮件来确认我的订单。

但是,我根本找不到选择下拉菜单的元素。我试过driver.find_element_by_id,name,xpath,class_name,css_selector,但都没有奏效。我也尝试过使用 select 选项,如下所示: select = Select(driver.find_element_by_id("quantity_2335182"))(quantity_2335182 是 Chrome 中检查元素中选择标签的 id),然后是 select.select_by_value('1')。它不断给出错误,例如:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="quantity_2335182"]"}
  (Session info: chrome=96.0.4664.93)

如果我不必像“quantity_2335182”那样指定特定的 id 并且可以根据通用类名、标签或 css_selector 选择下拉列表,我会更喜欢。另外,我只想选择两个下拉菜单中的第一个(仅编辑 Category1 而不是 Category2)。

【问题讨论】:

    标签: python html selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:
    wait=WebDriverWait(driver, 60)
    driver.get('https://www.tickettailor.com/events/testing4/621753')
    wait.until(EC.element_to_be_clickable((By.LINK_TEXT,"Join the guestlist"))).click()
    wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe#overlay_window")))
    row1=Select(wait.until(EC.presence_of_element_located((By.XPATH,"(//select)[1]"))))
    row1.select_by_index(1)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#submit"))).click()
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#name"))).send_keys("a")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#email"))).send_keys("a")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#email_confirm"))).send_keys("a")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#submit"))).click()
    ## do captcha
    key=wait.until(EC.presence_of_element_located((By.CSS_SELECTOR,".h-captcha"))).get_attribute("data-sitekey")
    print(key)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#submit"))).click()
    

    进口:

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

    要自动执行此操作,只需单击链接文本,切换到 iframe 覆盖窗口,使用选择导入和按索引选择,然后单击下一步按钮。

    【讨论】:

      【解决方案2】:

      您无法与下拉菜单交互,因为该弹出窗口位于 iframe 中。所以首先你必须切换到 iframe,然后你才能与元素进行交互。

      这里,请看它在 iframe 内:

      您的代码步骤是:

      1. 点击加入客人名单按钮
      2. 切换到 iframe。即driver.switch_to_frame("overlay_window")
      3. 根据您的要求与您的元素或下拉菜单交互
      4. 完成所有弹出操作后,切换到默认内容,即driver.switch_to.default_content()

      【讨论】:

        猜你喜欢
        • 2020-11-16
        • 1970-01-01
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 2017-06-16
        • 1970-01-01
        • 2020-12-11
        • 1970-01-01
        相关资源
        最近更新 更多