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?

5
  • Are you seeing any error? Commented Jul 17, 2022 at 10:43
  • What is the url you are trying to scrape? Commented Jul 17, 2022 at 11:13
  • When I run the module I get the following: selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: An invalid or illegal selector was specified Commented Jul 18, 2022 at 17:45
  • onecall.linesearchbeforeudig.co.uk/uk-b4-en/WorkBench - you'll need an account first :( Commented Jul 18, 2022 at 17:48
  • But it's free!! Commented Jul 18, 2022 at 17:48

1 Answer 1

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.