1

I am trying to select a dropdown option in the form using Selenium webdriver in Python. The XPATH is correct, I also verified it is going to the right dropdown option but in the end it is not selecting it.

I have tried similar code for another website that has a dropdown. But it's not working for this particular website.

Can someone please help out with this?

from selenium import webdriver driver = webdriver.Chrome("C:\\Users\\xxx\\Downloads\\chromedriver_win32\\chromedriver.exe") driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg") elem = driver.find_element_by_xpath("//select[@id='borrowerType']") all_options = elem.find_elements_by_tag_name("option") for option in all_options: if option.get_attribute("value") == "IND": option.click() break 
0

    2 Answers 2

    1

    You should add a wait before accessing the dropdown element to make it loaded.
    Also, this is a Select element, you can treat it in a special way as below:

    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 driver = webdriver.Chrome("C:\\Users\\xxx\\Downloads\\chromedriver_win32\\chromedriver.exe") wait = WebDriverWait(driver, 20) driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg") wait.until(EC.visibility_of_element_located((By.XPATH, "//select[@id='borrowerType']"))) select = Select(driver.find_element_by_xpath("//select[@id='borrowerType']")) # select by visible text select.select_by_value('IND') 
    10
    • Thanks for the suggestion. I actually tried to add a 10 second time wait but it is still not working. Dropdown values are getting loaded as soon as the page is loaded. Not sure what I am missing here.
      – Giffen
      CommentedAug 23, 2021 at 11:15
    • Are you sure you added the wait in correct way? Have you tried to use this code, what I wrote here?
      – Prophet
      CommentedAug 23, 2021 at 11:17
    • link - tried this code. It doesn't work. Do you think there's anything wrong with it?
      – Giffen
      CommentedAug 23, 2021 at 11:22
    • 1
      @Prophet : it's strange, I did try with explicit wait and action chains, nothing happened. there's no iframe either.CommentedAug 23, 2021 at 12:24
    • 1
      also I notice one thing, with automation the second drop down is getting highlighted but no element got selected whereas with manual run there's no highlight on both the drop down. Anyway upvoted for your efforts. I will continue to look into this for sometime now.CommentedAug 23, 2021 at 12:28
    0

    It's strange that Select class did not work. It needs a JavaScript call.

    driver = webdriver.Chrome(driver_path) driver.maximize_window() driver.implicitly_wait(50) driver.get("https://www.cersai.org.in/CERSAI/dbtrsrch.prg") driver.execute_script("return document.getElementById('borrowerType').selectedIndex = '2'") 
    2
    • wow, this works. thanks a ton. there's one more challenge though. If you notice, when trying manually, if we select any of the dropdown from "type of debtor", it adds new set of fields in the form. However, when we are automating the dropdown selection, it does not add any new fields
      – Giffen
      CommentedAug 23, 2021 at 13:16
    • @Giffen : I did look into, but got CERSAI Central Registry of Securitisation Asset Reconstruction and Security Interest of India The CERSAI site is under maintenance. The site will not be available from 23-Aug-2021 09:00 PM till 24-Aug-2021 12:00 AM .CommentedAug 23, 2021 at 15:33

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.