如何使用Selenium和Python测试自动完成

问题描述:

我已经写了几个使用硒为我的网站的测试。但是,我还没有找到一种方法来测试由自动完成填写输入文本框。举个例子,我想选择显示在自动完成列表下拉的地点之一。

I've been writing a couple of tests using selenium for my website. However, I've not found a way to test an input textfield that is filled by auto completion. As an example, I want to select one of the locations that is displayed in an auto-complete list dropdown.

这是我迄今为止:

driver.find_element_by_id("id_location_0").send_keys("Free City")
driver.find_element_by_id("ui-active-menuitem").click()

如何自动完成/自动建议在使用Python实现硒测试?

How is Auto-complete/Auto-suggest testing achieved in selenium using Python?

注意:谷歌搜索是什么,我试图做一个很好的例子。

NOTE: Google search is a good example of what I'm trying to do.

以下code为我工作:

The following code worked for me:

driver.find_element_by_id("id_location_0").send_keys("Free City")
element_to_hover_over = driver.find_element_by_css_selector("ul.ui-autocomplete > :first-child ")
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
driver.find_element_by_id("ui-active-menuitem").click()

我敢肯定,这可以在某种程度上进行优化。

I'm sure this can be optimized in some way.