Python 试图获取selenium代码以选择下拉菜单上的选项,但不起作用(Google表单)
我似乎无法让这个工作,我已经尝试了以下多个指南试图让这个工作2天了,我知道谷歌TOS,我不打算使用这个系统,我只是想试试 目标是让它自动从下拉菜单中选择一个选项,但是,它会不断出错 我尝试过xpath、jsname等,但没有任何效果,下面是错误和代码Python 试图获取selenium代码以选择下拉菜单上的选项,但不起作用(Google表单),python,selenium,Python,Selenium,我似乎无法让这个工作,我已经尝试了以下多个指南试图让这个工作2天了,我知道谷歌TOS,我不打算使用这个系统,我只是想试试 目标是让它自动从下拉菜单中选择一个选项,但是,它会不断出错 我尝试过xpath、jsname等,但没有任何效果,下面是错误和代码 while i < len(name): browser.find_element_by_xpath(inputName).send_keys(name[i]) time.sleep(1) pat
while i < len(name):
browser.find_element_by_xpath(inputName).send_keys(name[i])
time.sleep(1)
path = '//*[@id="mG61Hd"]/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div[1]/div[1]/span'
element = driver.find_element_by_xpath(path)
element.click()
time.sleep(1)
option_variable = 'A'
option_path = "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup appsMaterialWizMenuPaperselectPopup']//div[@class='quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption']//span[contains(text(), option_variable)]"
option = driver.find_element_by_xpath(option_path)
option.click()
browser.find_element_by_xpath(radioAnswer1).click()
browser.find_element_by_xpath(radioAnswer2).click()
browser.find_element_by_xpath(radioAnswer3).click()
browser.find_element_by_xpath(radioAnswer4).click()
sleep()
browser.find_element_by_xpath(submit).click()
i += 1
sleep()
试试这个
driver = webdriver.Chrome('./chromedriver')
web = "https://docs.google.com/forms/d/e/1FAIpQLSflbW3Y2LpAu6DTkJ4KjwDayYaAtvrFT5k8QRe2kJPWrjbbAg/viewform"
driver.get(web)
time.sleep(1)
path = "//div[@class='quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption isSelected isPlaceholder']"
element = driver.find_element_by_xpath(path)
element.click()
time.sleep(1)
option_variable = 'A'
option_path = "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup appsMaterialWizMenuPaperselectPopup']//div[@class='quantumWizMenuPaperselectOption appsMaterialWizMenuPaperselectOption freebirdThemedSelectOptionDarkerDisabled exportOption']//span[contains(text(), option_variable)]"
option = driver.find_element_by_xpath(option_path)
option.click()
路径是指向下拉按钮本身的路径。选项\u path是下拉列表中选项的路径
[第1版]
我稍微改变了一下选项路径。还添加了两个time.sleep来停止驱动程序窗体,立即运行下一个命令。这似乎效果更好
[编辑二]
添加了选项_变量。这允许我们指定要单击的选项。也相应地更改了选项_路径
[编辑部3]
更新了路径。这与之前发布的链接不同。问题可能出在按钮的路径上。共享您试图从中获取数据的链接,以便我们可以帮助您更好地保存链接请发送原始表单,删除所有个人信息在此之前,请尝试此xpath,button=///span[text='Choose']我找不到该按钮,因为google不使用按钮,我很确定。我给了我一个错误,现在整个代码都不起作用了。只是尝试了你的代码,我经常会遇到这种错误。selenium.common.exceptions.NoSuchElementException:Message:没有这样的元素:找不到元素:{method:xpath,selector://div[@class='QuantumwizMenuPaperSelectionList']}你是否正在尝试你在评论中发布的链接?这似乎在我的系统中起作用。我还添加了一个变量来选择必要的选项。你能用新代码更新你的问题吗?也许你错过了什么。这在我的系统中运行良好我更新了路径。这与新链接不同。试试这个