[英]Select DropDown value using Python Selenium
I am trying to select a dropdown value using Selenium in Python but not able to do so.我正在尝试使用 Python 中的 Selenium 来 select 下拉值,但无法这样做。 The code I get from "Copy Selector" is this.我从“复制选择器”得到的代码是这样的。
#mui-12848
The complete HTML is完整的 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">
I have tried我努力了
s1 = Select(browser.find_element_by_id("mui-12848"))
s1.select_by_visible_text('Quarterly')
which gives the following error UnexpectedTagNameException: Message: Select only works on elements, not on这给出了以下错误UnexpectedTagNameException: Message: Select 仅适用于元素,不适用于
I have also tried我也试过
browser.find_element(By.XPATH("//*[@id='mui-12848'][2]")).click();
which gives the following error TypeError: 'str' object is not callable给出以下错误TypeError: 'str' object is not callable
Any help is appreciated.任何帮助表示赞赏。
Try using expected_conditions
.尝试使用expected_conditions
。 See below.见下文。 Replace browser =.....
with your code.将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()
Your input
type for that HTML element is text
, it's not a Select
or a dropdown.该 HTML 元素的input
类型是text
,它不是Select
或下拉菜单。 The selenium class supports Select
. selenium class 支持Select
。
This error message...此错误消息...
UnexpectedTagNameException: Message: Select only works on elements, not on
...implies that you tried to use Select()
class which works only on <select>
element where as the desired element was an <input>
element. ...暗示您尝试使用Select()
class ,它仅适用于<select>
元素,其中所需的元素是<input>
元素。
To click on the <input>
element you can use either of the following Locator Strategies :要单击<input>
元素,您可以使用以下任一定位器策略:
Using css_selector
:使用css_selector
:
driver.find_element_by_css_selector("input[class*='MuiInputBase-input'][id^='mui'][value='Monthly']").click()
Using xpath
:使用xpath
:
driver.find_element_by_xpath("//input[contains(@class, 'MuiInputBase-input') and starts-with(@id, 'mui')][@value='Monthly']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable()
and you can use either of the following Locator Strategies :理想情况下,要单击需要为element_to_be_clickable()
诱导WebDriverWait的元素,您可以使用以下任一Locator Strategies :
Using CSS_SELECTOR
:使用CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[class*='MuiInputBase-input'][id^='mui'][value='Monthly']"))).click()
Using XPATH
:使用XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[contains(@class, 'MuiInputBase-input') and starts-with(@id, 'mui')][@value='Monthly']"))).click()
Note : You have to add the following imports:注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
The syntax is incorrect.语法不正确。 It should be like driver.find_element(By.XPATH, "//*[@id='mui-12848']").click()
它应该像driver.find_element(By.XPATH, "//*[@id='mui-12848']").click()
Moreover, you cannot include the index inside the locator.此外,您不能在定位器中包含索引。 You will need to use find_elements
first and then use the index on top of that: driver.find_elements(By.XPATH,"//*[@id='mui-12848']")[2].click()
您需要先使用find_elements
,然后在其上使用索引: driver.find_elements(By.XPATH,"//*[@id='mui-12848']")[2].click()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.