【发布时间】: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 <option> 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 附近是否有
<ul>和一组<li>?
标签: python selenium select drop-down-menu