如何使用Selenium和Python测试自动完成 [英] How to test autocomplete using selenium and Python

查看:136
本文介绍了如何使用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.

这篇关于如何使用Selenium和Python测试自动完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆