繁体   English   中英

Selenium 有没有办法从谷歌表单(python)的下拉菜单中选择一个选项

[英]Is there a way in Selenium to select an option from a drop down menu in google forms (python)

谷歌表单中的下拉菜单不使用任何标签,而是使用标签和 java。 我一直无法找到可以从谷歌表单下拉菜单中选择的任何内容。 他们的下拉菜单的 HTML 太长了,所以为了空间起见,我将提供一个示例 google 表单。

链接到示例

我通常通过等到 Selenium 找到下拉菜单来处理这个问题,然后使用宏或 RPA 工具(如 Approbotic)单击它并浏览选项(同时调整每次单击或选项移动之间的时间)。 这样的事情应该适合你:

import win32com.client
x = win32com.client.Dispatch("AppRobotic.API")
from selenium import webdriver

# navigate to Google Form
driver = webdriver.Firefox()
driver.get('https://forms.gle/SZftJSkvy9YWbktF9') 
# sleep 1 second
x.Wait(1000)

link = driver.find_element_by_link_text('Mail')
if len(link) > 0
    link[0].click()
# sleep 1 second
x.Wait(1000)
# press down arrow key
x.PressDownArrow
x.Wait(100)
x.PressDownArrow
x.Wait(100)

此页面具有自定义选择和选项,而不是默认选项。 您应该像使用常规 Web 元素一样使用它,只需使用常规定位器来查找元素然后进行交互。

尝试这个:

driver = webdriver.Chrome()
driver.get("https://docs.google.com/forms/d/e/1FAIpQLSdpyJ9UBFtsQDZHhK7KsYuILm5kh68jvY5DeFAKIBPTxx4RCQ/viewform")

driver.implicitly_wait(4)
# Click on top option placeholder to open a drop down:
driver.find_element_by_xpath("//div[@role='option' and contains(@class, 'isPlaceholder')]").click()

sleep(1) # Wait for options to load
options = driver.find_elements_by_xpath("//div[@role='option' and not(contains(@class, 'isPlaceholder'))]")
# And now, let's click on the 4th one by index:
options[3].click()

希望这有帮助,好舔!

亲爱的试试这个,我认为这会奏效

    from selenium import webdriver 
    import time


    driver = webdriver.Chrome("chromedriver/chromedriver")
    driver.get('https://docs.google.com/forms/d/e/1FAIpQLSdpyJ9UBFtsQDZHhK7KsYuILm5kh68jvY5DeFAKIBPTxx4RCQ/viewform')
'''For click drop down'''
    driver.find_element_by_xpath('//*[@id="mG61Hd"]/div/div/div[2]/div/div/div[2]').click()
'''Time for wait --> 1 second'''
    time.sleep(1)
'''Select the option '''
    driver.find_element_by_xpath('//*[@id="mG61Hd"]/div/div/div[2]/div/div/div[2]/div[2]/div[4]/span').click()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM