Home > Blockchain >  Unable to select any option from a dropdown list [Python Selenium] based on combobox
Unable to select any option from a dropdown list [Python Selenium] based on combobox

Time:02-03

Trying to automate one thing for my work, which was choosing one option from the dropdown list on the website below:

https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ

the Selenium automation didn't work in that case. After writing such code:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_driver_path = r"C:/Users/.../Projects/chromedriver.exe"

service = Service(executable_path=chrome_driver_path)
driver = webdriver.Chrome(service=service)
driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
wait = WebDriverWait(driver, 10)
abo_button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="combobox-button-53"]')))

After executing I've got a message:

TimeoutException

In case of finding element by tag name or any other options, the following message pops up:

Message: no such element: Unable to locate element

The list is built on button tags and has a structure of lightning-basecombobox. It looks like there is no possibility to click on the dropdown list and choose the required option automatically.

Is it needed to do something different with such stuff?

What I expect is to use Selenium to choose between the options in the list.

CodePudding user response:

When I load the page, I get the element ID (XPATH) as following. //*[@id="combobox-button-51"]

Maybe the number 51 isn't always the same? In that case, try:

//*[starts-with(@id,'combobox-button-5')]

Or just use //*[@name="subscriptionsTypeId"] , as another answer here allready mentioned.

CodePudding user response:

The combobox is a <button> element.

combobox

Additionally the id i.e. combobox-button-53 is dynamically generated and is bound to chage sooner/later. They may change next time you access the application afresh or even while next application startup. So can't be used in locators.


Solution

To click 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:

    driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[name='subscriptionsTypeId']"))).click()
    
  • Using XPATH:

    driver.get("https://interparking-pl.my.site.com/abonament/s/?id=a0A58000000D7pZ")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@name='subscriptionsTypeId']"))).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
    
  • Browser Snapshot;

combobox_expanded

  • Related