1

I am trying to select a dropdown value using Selenium in Python but not able to do so. The code I get from "Copy Selector" is this.

#mui-12848

The complete HTML is

<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

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

Any help is appreciated.

Following is the Screenshot The Screenshot of the DropDown Component

5
  • Your element is not a SELECT but an INPUT. Commented Dec 28, 2020 at 17:52
  • stackoverflow.com/questions/62619301/… Commented Dec 28, 2020 at 17:53
  • @Mayank You need to provide the html of the list! the input you are showing is not the desired element! You can provide the URL or the html and then we can help. Commented Dec 29, 2020 at 12:20
  • @MosheSlavin I have pasted the html in the question... Do you mean there is any other HTML I need to provide? Commented Dec 29, 2020 at 16:19
  • @Mayank Yes, the list should be in a different element Commented Dec 29, 2020 at 16:33

4 Answers 4

0

Your input type for that HTML element is text, it's not a Select or a dropdown. The selenium class supports Select.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

To click on the <input> element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("input[class*='MuiInputBase-input'][id^='mui'][value='Monthly']").click()
    
  • Using 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:

  • Using 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:

    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
    

2 Comments

Hi, thanks for your reply. I tried both of your approaches of "CSS_Selector" and "XPATH". In both of them, I got this error "TimeoutException: Message: "
I have added the screenshot if it is of any help
0

The syntax is incorrect. It should be like 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()

4 Comments

Hi, thanks for your reply. I tried your approach but I got this error "InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression ////*[@id='mui-12848'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '////*[@id='mui-12848']' is not a valid XPath expression."
I have added the screenshot if it is of any help
There should be only two backslash //. I have made that change in my answer. Also, id in your screenshot is different than the one I used in my code snippet, so make sure to update that as well.
Also, since it's input type, try directly with: driver.find_element(By.XPATH, "//*[@id='mui-23895']").send_keys("Yearly")
0

Try using expected_conditions. See below. Replace browser = ..... with your code.

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()

3 Comments

Hi, thanks for your reply. I tried your approach but I got the error "TimeoutException: Message: "
I have added the screenshot if it is of any help
@Mayank I updated the answer to use presence_of_element_located instead of element_to_be_clickable. If that does not work then the xpath needs to change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.