【问题标题】:How to select an option from dropdown not having Select tag using Selenium through Python?python - 如何通过Python从没有使用Selenium的Select标签的下拉列表中选择一个选项?
【发布时间】:2019-10-18 15:02:37
【问题描述】:

我正在尝试学习 selenium,但遇到了单击下拉选项的问题。非常沮丧,我为此问题创建了一个帐户。

网址https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform

我添加了时间功能部分,考虑是否可以单击选项加载。不幸的是,它失败了。

从硒导入网络驱动程序

browser = webdriver.Chrome()
browser.implicitly_wait(5)
browser.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform?usp=sf_link")
start =browser.find_element_by_xpath('//*[@id="mG61Hd"]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[1]/span')

start.click()
import time 
time.sleep(2)
startt =browser.find_element_by_xpath('//*[@id="mG61Hd"]/div/div[2]/div[2]/div[2]/div/div[2]/div[1]/div[1]/div[6]/span')
time.sleep(2)
startt.click()

【问题讨论】:

    标签: python-3.x selenium xpath css-selectors webdriverwait


    【解决方案1】:

    click() 元素上的文本为 Next,您需要诱导 WebDriverWait 以使 元素可点击,您可以使用以下任一Locator Strategies:

    • 使用CSS_SELECTOR

      driver.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.quantumWizMenuPaperselectOption.freebirdThemedSelectOptionDarkerDisabled.exportOption.isSelected.isPlaceholder span.quantumWizMenuPaperselectContent.exportContent"))).click()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.exportSelectPopup.quantumWizMenuPaperselectPopup div[data-value='Option 4'] span.quantumWizMenuPaperselectContent.exportContent"))).click()
      
    • 使用XPATH

      driver.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E-sDce1Dp7dRKm9iWqw/viewform")
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='quantumWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder']//span[@class='quantumWizMenuPaperselectContent exportContent']"))).click()
      WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup']//span[@class='quantumWizMenuPaperselectContent exportContent' and text()='Option 4']"))).click()
      

    【讨论】:

      【解决方案2】:

      这是您可以用来选择选项的逻辑。

      # this will open the list box
      driver.execute_script("arguments[0].click()",driver.find_element_by_xpath("(//div[@role='option'])[1]"))
      
      # this will select the option
      driver.find_element_by_xpath("(//div[@role='listitem']//span[.='Option 4'])[2]").click()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        • 1970-01-01
        • 2021-05-24
        相关资源
        最近更新 更多