如何通过Python使用Selenium从没有Select标签的下拉列表中选择一个选项?

时间:2019-06-03 03:44:01

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

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

URL 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()

2 个答案:

答案 0 :(得分:0)

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

brown

答案 1 :(得分:0)

要在文本为 Next 的元素上click(),请诱使 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()