LucSpan
LucSpan

Reputation: 1971

Select dropdown option via Selenium

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(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.

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.

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?

Upvotes: 0

Views: 667

Answers (2)

JM217
JM217

Reputation: 737

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;
    }

Upvotes: 0

SnR
SnR

Reputation: 319

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")

Upvotes: 1

Related Questions