Reputation: 405
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()
Upvotes: 0
Views: 57
Reputation: 193108
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
You can find a couple of relevant discussions in:
Upvotes: 1