Home > other >  How can I select item from dropdown, when option's element not interactable via selenium/python
How can I select item from dropdown, when option's element not interactable via selenium/python

Time:04-06

I want to select the item from the dropdown, but the options which are to be selected are not interactable, and show (Alert: This element is not interactable through selenium(automation) as it is not visible in UI. Try any near by element. Learn more...) alert.

Here the Html

<div >
  <div >
      <select  id="coursedd" name="title">
          <option disabled="disabled" selected="selected">Select course</option>
                                            
            <option value="1">Class A CDL</option>
                                            
            <option value="2">Test Course Title B</option>
                                            
            <option value="5">NA</option>
                                            
            <option value="6">NA</option>
                                            
          <option value="7">NA</option>
                                            
        </select>
</div>

I tried with this method but it's not working

select_course = Select(driver.find_element(By.XPATH, "//input[@id='cpassword']"))
select_course.select_by_value('2')

CodePudding user response:

This works per the HTML you have provided:

sel = Select(WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.ID, "coursedd"))))
by_val = sel.select_by_value('2')
print(sel.first_selected_option.text)

Additional imports:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

Output:

Test Course Title B

Process finished with exit code 0
  • Related