0

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?

3
  • You're trying to wait for hidden element to be clickable... Remove that line and try again. What is el_id? Commented Jan 2, 2019 at 9:30
  • el_id() = browser.find_element_by_id(). I'll have a look. Commented Jan 2, 2019 at 10:19
  • Can you cross check if there is a <ul> and a set of <li> near around the HTML you have provided with similar set of options? Commented Jan 2, 2019 at 13:47

2 Answers 2

1

you need to use either select_by_value() or select_by_visibletext() option.For example in order to select the option Finland you can use :

 dropdown = Select(el_id('shipping_country'))
//either
 dropdown.select_by_value("FI")  
//or
 dropdown.select_by_visibletext("Finland")
Sign up to request clarification or add additional context in comments.

2 Comments

Then can u try opening the dropdown first & select the value?
That was my second try. Thank you for your effort, but your help could even be more useful if you read the explanation in my question first.
0

Why not just setting value attribute of the select tag? If you want to select 'Finland' for example, you can get the associated value first as follows.

I use C# mainly and the following code is in C# Selenium.

var state = 'Finland';
xpath = $"//*/select[@id='shipping_country']/option[text()='{state}']";
string value = Driver.FindElementByXPath(xpath).GetAttribute("value");
xpath = "//*/select[@id='shipping_country']";
await set_value(xpath, value);

The following function is what I normally use for setting value for input fields.

public async Task<string> set_value(string xpath, string val, string field = "value")
{
        Object node = null;
        string script = "(function()" +
                            "{" +
                                "node = document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" +
                                "if (node==null) return '" + m_err_str + "';" +
                                "node." + field + "=\"" + val + "\";" +
                                "return 'ok';" +
                        "})()";
        node = m_js.ExecuteScript(script);
        if (node != null)
            return node.ToString();
        return m_err_str;
    }

2 Comments

Thank you but I'm using Python and am not familiar with C#.
<I'm using Python and am not familiar with C#> - No worries. Finding element using XPath is supported in Python too and the main idea should apply fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.