【问题标题】:Select dropdown option via Selenium通过 Selenium 选择下拉选项
【发布时间】:2019-05-28 22:36:07
【问题描述】:

设置

我正在尝试从 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 &lt;option&gt; could not be scrolled into view

  • 选择 2

我也尝试过插入“等待”,

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

我该如何解决?

【问题讨论】:

  • 您正在尝试等待隐藏元素可点击...删除该行并重试。 el_id 是什么?
  • el_id() = browser.find_element_by_id()。我去看看。
  • 您能否交叉检查您提供的具有类似选项集的 HTML 附近是否有 &lt;ul&gt; 和一组 &lt;li&gt;

标签: python selenium select drop-down-menu


【解决方案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")

【讨论】:

  • 那你可以先打开下拉菜单并选择值吗?
  • 那是我的第二次尝试。感谢您的努力,但如果您先阅读我的问题中的解释,您的帮助可能会更有用。
【解决方案2】:

为什么不直接设置 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;
    }

【讨论】:

  • 谢谢,但我使用的是 Python,对 C# 不熟悉。
  • - 不用担心。 Python 也支持使用 XPath 查找元素,主要思想应该适用。
猜你喜欢
  • 1970-01-01
  • 2019-11-11
  • 2015-05-15
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多