Set-up
I'm trying to select a country from a WooCommerce drop-down menu.
<select name="shipping_country" id="shipping_country" class="country_to_state country_select select2-hidden-accessible" autocomplete="country" tabindex="-1" aria-hidden="true" style="">
<option value="">Selecteer een land…</option>
<option value="BE">België</option>
<option value="DE">Duitsland</option>
<option value="FI">Finland</option>
<option value="FR">Frankrijk</option>
<option value="HU">Hongarije</option>
<option value="NL" selected="selected">Nederland</option>
<option value="AT">Oostenrijk</option>
<option value="PL">Polen</option>
<option value="ES">Spanje</option>
<option value="GB">Verenigd Koninkrijk (UK)</option>
</select>
I've tried my usual way using Select() and have experimented with ActionChains, but to no avail.
Tries
- Select 1
Select(el_id('shipping_country')).select_by_value(latest_order['shipping']['country'])
where el_id() = browser.find_element_by_id() and latest_order['shipping']['country'] contains the 2 letter country code of shipping.
This gives ElementNotInteractableException: Element <option> could not be scrolled into view.
- Select 2
I've also tried to insert a 'wait',
dropdown = Select(el_id('shipping_country'))
wait.until(EC.element_to_be_clickable((
By.XPATH, "//select[@id='shipping_country']//options[contains(.," + latest_order['shipping']['country'] +")]")))
dropdown.select_by_value(latest_order['shipping']['country'])
where wait = WebDriverWait(browser, 10).
This gives TimeoutException.
- ActionChains
Based on an answer,
dropdown = el_xp("//select[@name='shipping_country']")
actions = ActionChains(browser)
actions.move_to_element(dropdown)
actions.click(dropdown)
select_box = Select(dropdown)
actions.move_to_element(select_box.select_by_value(latest_order['shipping']['country']))
This gives,
Traceback (most recent call last):
File "<ipython-input-43-a82c544929aa>", line 1, in <module>
actions.move_to_element(select_box.select_by_value(latest_order['shipping']['country']))
File "/Applications/anaconda/lib/python3.6/site-packages/selenium/webdriver/common/action_chains.py", line 289, in move_to_element
self.w3c_actions.pointer_action.move_to(to_element)
File "/Applications/anaconda/lib/python3.6/site-packages/selenium/webdriver/common/actions/pointer_actions.py", line 42, in move_to
raise AttributeError("move_to requires a WebElement")
AttributeError: move_to requires a WebElement
How do I solve this?
el_id?el_id() = browser.find_element_by_id(). I'll have a look.<ul>and a set of<li>near around the HTML you have provided with similar set of options?