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

Mayank
  • 93
  • 1
  • 9

4 Answers4

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()
Jortega
  • 3,616
  • 1
  • 18
  • 21
  • Hi, thanks for your reply. I tried your approach but I got the error "TimeoutException: Message: " – Mayank Dec 29 '20 at 04:06
  • I have added the screenshot if it is of any help – Mayank Dec 29 '20 at 04:14
  • @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. – Jortega Dec 29 '20 at 15:47
0

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

0xc0de
  • 8,028
  • 5
  • 49
  • 75
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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 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: " – Mayank Dec 29 '20 at 04:03
  • I have added the screenshot if it is of any help – Mayank Dec 29 '20 at 04:14
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()

MikeCode
  • 91
  • 11
  • 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." – Mayank Dec 29 '20 at 04:05
  • I have added the screenshot if it is of any help – Mayank Dec 29 '20 at 04:14
  • 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. – MikeCode Dec 29 '20 at 06:43
  • Also, since it's `input` type, try directly with: `driver.find_element(By.XPATH, "//*[@id='mui-23895']").send_keys("Yearly")` – MikeCode Dec 29 '20 at 06:52