简体   繁体   English

如何使用 selenium 使用 python 处理 Google 表单的下拉列表

[英]How to Handle Drop-down for Google form using selenium using python

can someone help me on how to automate the dropdown menu in google form using selenium in python. the problem that I am facing is I am able to locate the drop down menu and select it but I am not able to select the options I have tried select_by_index but it doesn't work.有人可以帮助我如何使用 python 中的 selenium 自动执行 google 表单中的下拉菜单吗?我面临的问题是我能够找到下拉菜单和 select 但我无法 select 我尝试过的选项select_by_index但它不起作用。 thank you in advance.先感谢您。 (I am new to the forum so sorry if I have asked the question in a ambiguous way) (我是论坛的新手,很抱歉,如果我以模棱两可的方式提出问题)

drop = Select(browser.find_element_by_class_name('quantumWizMenuPaperselectContent').click())
drop.select_by_index(0)

Although I think there may be better ways to do it, I manage to do it this way:虽然我认为可能有更好的方法来做到这一点,但我设法做到了:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome("YOUR PATH HERE")
driver.get("YOUR SITE HERE")
driver.find_element(By.CSS_SELECTOR, ".isSelected").click()

# Item 1 in drop down has an ending Div of 3
# Item 2 ending div of 4 
# I added the + 2 in the f string so that DROPDOWN_ITEM = 1 Means find for div 3

DROPDOWN_ITEM = 1
dropdown_item_1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located
                                       ((By.XPATH, f'//*[@id="mG61Hd"]/div[2]/div/div[2]/div/div/div/div[2]/div/div[2]/div[{DROPDOWN_ITEM} + 2]')))

dropdown_item_1.click()

if you would just like to target the first item, can use something like this:如果您只想定位第一项,可以使用以下内容:

dropdown_item_1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located
                                       ((By.XPATH, '//*[@id="mG61Hd"]/div[2]/div/div[2]/div/div/div/div[2]/div/div[2]/div[3]')))

dropdown_item_1.click()

Without using Select, here is how I managed to do it.在不使用 Select 的情况下,这是我设法做到的。

Since google forms are TAB friendly (ie, you can navigate top to bottom using the TAB key), you can select the previous field and do send_keys(Keys.TAB) to focus the target field...由于谷歌 forms 是 TAB 友好的(即,您可以使用 TAB 键从上到下导航),您可以 select 前一个字段并执行 send_keys(Keys.TAB) 以聚焦目标字段...

example:-例子:-

url = "https://docs.google.com/forms/d/e/1FAIpQLScosZjmDrvgUvh77tXsaAb24hKVgaBnjJfJz2BX1PvoqIO1Ow/viewform"

phField = #xpath of phone number field

now, focusing on the next field of phField, and sending the needed option.现在,关注 phField 的下一个字段,并发送所需的选项。

driver.find_element_by_xpath(phField ).send_keys(Keys.TAB,"10.05")

don't forget to import Keys from selenium.webdriver.common.keys不要忘记从 selenium.webdriver.common.keys 导入密钥

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

相关问题 使用硒处理 Google 表单的下拉列表 - Handling Drop-down for Google form using selenium 如何使用 Python 与 Selenium 一起 select 下拉菜单值? - How to select a drop-down menu value with Selenium using Python? UnexpectedTagNameException:选择仅适用于<select>元素,而不是“<form> ” 使用 Selenium 和 Python 选择下拉值时出错 - UnexpectedTagNameException: Select only works on <select> elements, not on “<form>” error selecting a drop-down value using Selenium and Python 使用selenium python从下拉选项中选择一个值 - Selecting a value from a drop-down option using selenium python 下拉列表选择使用 Selenium Python - Drop-down list selection using Selenium Python 使用 selenium Python 库单击下拉菜单中的项目 - Click an item in a drop-down menu using selenium Python library 使用Selenium和python进行依赖的DROP-DOWN选择 - Dependent DROP-DOWN selection using selenium and python 下拉是隐藏的,不知道如何使用 selenium python 处理它? - Drop down is hidden, Not sure how to handle it using selenium python? 如何在 Python 上使用 Selenium 处理下拉菜单? - How to handle drop down menus using Selenium on Python? 如何使用硒从下拉菜单中随机选择? - How to randomly select from a drop-down menu using selenium?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM