0

I've been going through a few other pages on stackoverflow but failed to find a solution that solves my problem now. I am trying to use selenium (python) to automate a work process. It involves logging in the web portal, selecting a few search criteria, and then download the files. I am stuck with the selection part. I can click on other dropdown menu on the same page by not this one:

    <select id="fMain:j_id162:rangeLineIdlocation1" name="fMain:j_id162:rangeLineIdlocation1" size="1" onchange="if(MMIS.Event.delay(this, arguments)) return true;A4J.AJAX.Submit('fMain',event,{'similarityGroupingId':'fMain:j_id162:j_id165','containerId':'fMain:j_id161','parameters':{'fMain:j_id162:j_id165':'fMain:j_id162:j_id165'} } )"> 
        <option value=" "> </option>
        <option value="10000109">AEL</option>
        <option value="10000123">AEL+TCL</option>
    </select>

I have tried using the method suggested in webdriver wait for ajax request in python


    try:
    element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="fMain:j_id165:rangeLineIdlocation1"]')))
finally:
    driver.find_element_by_xpath('//*[@id="fMain:j_id165:rangeLineIdlocation1"]/option[1]').click()

But it didn't work. I'm currently stuck in here now. Any clue? Thank you.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

Wait for element and then create a select type element.

element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, '//*[@id="fMain:j_id165:rangeLineIdlocation1"]')))
dropdown = Select(element )

Then use any of the below

By Index:

dropdown.select_by_index(0) #It will select options by index. in this case 0 =AEL, 1 = AEL+TCL

By Visible Text:

dropdown.select_by_visible_text("AEL") #It will match Inner Html to select option. in this case it will match AEL

By Value

dropdown.select_by_value("10000109") # It will use attribute value to select option
rahul rai
  • 2,260
  • 1
  • 7
  • 17
0

To select the <option> with text as AEL using Selenium you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR and select_by_visible_text():

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id*='rangeLineIdlocation1'][name*='rangeLineIdlocation1']"))))
    select.select_by_visible_text('AEL')
    
  • Using XPATH and select_by_value():

    select = Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[contains(@id, "rangeLineIdlocation1")][contains(@name,'rangeLineIdlocation1')]"))))
    select.select_by_value('10000109')
    
  • 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