通过硒选择下拉选项

时间:2019-01-02 09:05:36

标签: python selenium select drop-down-menu

设置

我正在尝试从WooCommerce下拉菜单中选择一个国家/地区。

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

我已经尝试过使用Select()的常规方法,并尝试了ActionChains,但无济于事。


尝试

  • 选择1

Select(el_id('shipping_country')).select_by_value(latest_order['shipping']['country'])

其中el_id() = browser.find_element_by_id()latest_order['shipping']['country']包含2个字母的运输国家代码。

这给出了ElementNotInteractableException: Element <option> could not be scrolled into view

  • 选择2

我也尝试插入“ 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'])

其中wait = WebDriverWait(browser, 10)

这给出了TimeoutException

  • 动作链

基于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']))

这给出了

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

我该如何解决?

2 个答案:

答案 0 :(得分:1)

您需要使用select_by_value()或select_by_visibletext()选项。例如,要选择芬兰选项,可以使用:

 dropdown = Select(el_id('shipping_country'))
//either
 dropdown.select_by_value("FI")  
//or
 dropdown.select_by_visibletext("Finland")

答案 1 :(得分:0)

为什么不仅仅设置select标签的value属性? 例如,如果要选择“芬兰”,则可以首先获取相关的值,如下所示。

我主要使用C#,下面的代码在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);

以下功能通常是我用来设置输入字段值的功能。

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