0

I'm trying to select country but somehow it's not working. The code below is what I did. It's not working properly.

<select name="state" class="form-control selectpicker">
                                        <option value=" ">Please select your state</option>
                                        <option>Alabama</option>
                                        <option>Alaska</option>
                                        <option>Arizona</option>
                                        <option>Arkansas</option>
                                        <option>California</option>
                                        <option>Colorado</option>
                                     </select>

Here what I did: browser.find_element_by_css_selector(".form-control.selectpicker [option='Alaska']").click()

1 Answer 1

1

To select the <option> with text as Alaska you can use the following Locator Strategy:

  • Using xpath:

    dropdown_menu = Select(browser.find_element_by_xpath("//select[@class='form-control selectpicker' and @name='state']"))
    dropdown_menu.select_by_visible_text('Alaska')
    

Ideally, you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategy:

  • Using CSS_SELECTOR and in a single line:

    Select(WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select.form-control.selectpicker[name='state']")))).select_by_visible_text('Alaska')
    
  • 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
    

Reference

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.