使用硒处理Google表单的下拉菜单 [英] Handling Drop-down for Google form using selenium
问题描述
可以帮助如何自动在Google Forms中进行下拉菜单.当我尝试为下拉菜单编写X-Path而不是使用Select和option元素时,它先后是div元素和content元素.访问xpath并使用sendKeys发送值会引发错误.
can some one help how to automate drop-downs in Google Forms.As when i tried writing the X-Path's for the drop down instead of having Select and option elements it has div element followed by content element.Also when i try to access the xpath and send the values using sendKeys it's throwing an error.
推荐答案
而不是使用 sendKeys
,您应该使用 Select()
进行下拉,如下所示:
instead of using sendKeys
you should use Select()
for drop down as below:
例如如果您将性别设置为下拉菜单,并且需要选择性别,则可以选择以下内容:
Ex. If you have gender as drop down and you need to select gender you can select as below:
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.webdriver import WebDriver
driver = WebDriver() # setup web driver
driver.get(<url>) # retrive url
gender_select = Select(driver.find_element_by_name('gender')) # get element by name or id or xpath
gender_select.select_by_visible_text('Male') # here 'Male' is the text displayed on page so you can select item from dropdown menu by text visible in drop down menu
更新:
选择
将不起作用,这是实时演示:
Select
won't work if tag is not select for your purpose here's the live demo:
def foo(url="https://docs.google.com/forms/d/e/1FAIpQLScbs4_3hPNYgjUO-hIa-H1OfJiDZ-FIY1WSk31jGyW5UtQ-Ow/viewform", opt="Option 2", delay=20):
from selenium.webdriver.chrome.webdriver import WebDriver
import time
driver = WebDriver()
driver.get(url)
driver.find_element_by_class_name("quantumWizMenuPaperselectOptionList").click()
options=driver.find_element_by_class_name("exportSelectPopup")
time.sleep(3)
print(options)
contents = options.find_elements_by_tag_name('content')
[i.click() for i in contents if i.text == opt]
foo()
这篇关于使用硒处理Google表单的下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!