python - 使用 Python Selenium 选择下拉值
问题描述
我正在尝试在 Python 中使用 Selenium 选择下拉值,但无法这样做。我从“复制选择器”得到的代码是这样的。
#mui-12848
完整的 HTML 是
<input aria-invalid="false" autocomplete="off" type="text" class="MuiInputBase-input MuiOutlinedInput-input MuiAutocomplete-input Reports-autocompleteInput-133 MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd MuiOutlinedInput-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="Monthly" id="mui-12848" aria-activedescendant="mui-12848-option-1" aria-controls="mui-12848-popup">
我努力了
s1 = Select(browser.find_element_by_id("mui-12848"))
s1.select_by_visible_text('Quarterly')
这给出了以下错误 UnexpectedTagNameException: Message: Select only works on elements, not on
我也试过
browser.find_element(By.XPATH("//*[@id='mui-12848'][2]")).click();
这给出了以下错误 TypeError: 'str' object is not callable
任何帮助表示赞赏。
解决方案
尝试使用expected_conditions
. 见下文。替换browser = .....
为您的代码。
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
browser = .....
# ADD YOUR CODE TO GET TO THE PAGE WITH THE BUTTON
to_click = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH, "//*[@id='mui-12848'][2]")))
to_click.click()
推荐阅读
- html - JSP,如何根据它是哪个表单来更改HTML表单名称?
- python - Is there a way to extract pixel color information from an image with OpenImageIO's python bindings
- python - Tile rendering with opengl
- python - Create virtual environment with all packages shown in `conda list`
- c# - code throws Object synchronization method was called from an unsynchronized block of code
- javascript - 如何修复 JS 中的 insertBefore() 错误
- imagemagick - 将当前日期添加到签名图像并将其添加到现有 pdf
- google-apps-script - Automatically generate a unique sequential ID in Google Sheets
- javascript - Is it possible to filter on multiple map values in Firestore
- python - 如何有效地计算 pandas 中所有后续行的平均值?