首页 > 解决方案 > 使用 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

任何帮助表示赞赏。

以下是截图 DropDown 组件截图

标签: pythonseleniumselenium-webdriver

解决方案


尝试使用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()

推荐阅读