1

I want to select the visible text 'planned works' from the following drop-down list HTML:

<select id="NewEnquiry.Purpose" data-bind="options: SelectablePurposes, value: Purpose, optionsText: 'Name', optionsCaption: '-- Please Select --', validationOptions: { rule: 'Enquiry.Purpose' }"><option value="">-- Please Select --</option><option value="">Planned Works</option><option value="">Initial Enquiry</option><option value="">Emergency</option></select>
<option value>-- Please Select --</option>
<option value>Planned Works</option>
<option value>Initial Enquiry</option>
<option value>Emergency</option>

The code I have been using, hasn't been working:

select = Select(driver.find_element_by_id('//*[@id="NewEnquiry.Purpose"]'))
select.select_by_visible_text('Planned Works')

and using the following import:

from selenium.webdriver.support.ui import Select

Any suggestions?

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

1 Answers1

1

The <select> element seems to be a dynamic element. So to select the <option> with text as Planned Works you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select[id^='NewEnquiry'][data-bind*='SelectablePurposes']")))).select_by_visible_text('Planned Works')
    
  • Using XPATH:

    Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[starts-with(@id, 'NewEnquiry') and contains(@data-bind,'SelectablePurposes')]")))).select_by_visible_text('Planned Works')
    
  • 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
    

References

You can find a couple of relevant discussions in:

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