簡體   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