Python 3.x 如何通过Python使用Selenium从没有select标记的下拉列表中选择选项?
我试图学习selenium,但遇到了单击下拉选项的问题。我非常沮丧,为此问题创建了一个帐户 网址 我添加了时间函数部分,认为如果加载选项,我可以单击它。不幸的是,这是一次失败 从selenium导入webdriverPython 3.x 如何通过Python使用Selenium从没有select标记的下拉列表中选择选项?,python-3.x,selenium,xpath,css-selectors,webdriverwait,Python 3.x,Selenium,Xpath,Css Selectors,Webdriverwait,我试图学习selenium,但遇到了单击下拉选项的问题。我非常沮丧,为此问题创建了一个帐户 网址 我添加了时间函数部分,认为如果加载选项,我可以单击它。不幸的是,这是一次失败 从selenium导入webdriver browser = webdriver.Chrome() browser.implicitly_wait(5) browser.get("https://docs.google.com/forms/d/e/1FAIpQLSc-3miqMb1Dixi7v4Le-2_SXIzekf0E
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()
下面是您可以用来选择选项的逻辑
# 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()
要在文本为“下一步”的元素上单击(),您需要引导WebDriverWait使该元素可单击,并且可以使用以下任一选项:
- 使用
:CSS\u选择器
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()