Selenium中是否有一种方法可以从Google表单(python)的下拉菜单中选择一个选项,

问题描述 投票:0回答:3

Google表单中的下拉菜单不使用任何标签,而使用标签和Java。我一直无法找到可以从Google表单的下拉菜单中选择的任何内容。他们的下拉菜单的HTML太长了,因此为了节省空间,我将提供一个Google表单示例。

Link to the example

python html selenium drop-down-menu google-form
3个回答
1
投票

我通常通过等待,直到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)

0
投票

此页面具有自定义选择和选项,不是默认选项。您应该像使用常规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()

希望有帮助,请舔一下!


0
投票

亲爱的尝试一下,我认为这将会起作用

    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()
© www.soinside.com 2019 - 2024. All rights reserved.