问题
I have been trying to find a way to select a dropdown option from google forms through selenium and python but have thus far been unsuccessful.
I tried several ways including the Select class (which does not work due to the dropdown form not using the select tag). As well as XPATH. But have only got so far as to click on the dropdown but not being able to select the options within the said dropdown list. Any help would be greatly appreciated!
I need to be able to specify which dropdown option to choose based on the text/value in the various options.
CODE:
browser.find_element_by_xpath("//div[@class='quantumWizMenuPaperselectOptionList']").click()
browser.find_element_by_xpath("//div[@class='freebirdThemedSelectOptionDarkerDisabled']/div[@class='quantumWizMenuPaperselectOption'][@data-value='1.05pm - 3.55pm']").click()
The error I get is that no such element is found even though this is the XPATH I have found from inspecting the dropdown menu accordingly.
I have created an example form here for reference: https://forms.gle/prBMqgVVFNv5KWQQA
These did not help as it uses the Select class, please do not flag this question as a duplicate as such
How to select/get drop down option in Selenium 2
How to select a drop-down menu value with Selenium using Python?
Using a webdriverwait also did not seem to do the trick as detailed in this post:
Automate Dropdown Menu in Selenium Without Select
回答1:
The problem is the selection box wont popup even if your xpath is correct until you hover on that element, hovering on that element makes it clickable. You can use below code to hover on that selection box element and than try to click and select element
selectBox = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@role='listbox']")))
action = ActionChains(browser);
action.move_to_element(selectBox).perform()
Full code here:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
url = 'https://docs.google.com/forms/d/e/1FAIpQLScosZjmDrvgUvh77tXsaAb24hKVgaBnjJfJz2BX1PvoqIO1Ow/viewform'
text = '1.05pm-3.55pm'
browser.maximize_window()
browser.get(url)
selectBox = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@role='listbox']")))
action = ActionChains(browser);
action.move_to_element(selectBox).perform()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@role='listbox']")))
selectBox.click()
selectionXpath = "//div[@class='exportSelectPopup quantumWizMenuPaperselectPopup appsMaterialWizMenuPaperselectPopup']//span[@class='quantumWizMenuPaperselectContent exportContent' and text()='"+text+"']"
selection = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, selectionXpath)))
selection.click()
Note that I have not verified that this way of selection preserves the information in the form as I have not submitted the form. You can test it verify it.
来源:https://stackoverflow.com/questions/65015336/selecting-specifically-google-form-dropdown-using-selenium-without-select-tag-in