python - 填充部分文本后如何单击自动完成?
问题描述
我正在测试一个表单,它有一个类别字段,它是一个基于输入的下拉菜单。使用 .send_keys('text') 添加一些文本后,它会显示类别列表。看一下它的 HTML:
<input type="text" aria-required="true" id="categories" maxlength="64" value="" autocomplete="off" class="input__69f5f__1POmY" placeholder="Pizza (Be specific)">
我这样做是为了查找并提交输入文本:
categories = browser.find_element_by_id('categories').send_keys('Software Development')
之后,它会显示如下列表:
请有人可以帮助我,我如何单击下拉菜单中的选项?
我正在使用 Firefox 网络驱动程序。
谢谢。
解决方案
这不是一个优雅的解决方案,但我会编写类似下面的代码来从下拉列表中选择项目。
import time
from selenium.webdriver import Chrome
driver = Chrome()
driver.get('https://biz.yelp.com/signup_business/new')
categories_input = driver.find_element_by_id('categories')
categories_input.send_keys('Professional')
time.sleep(5) # replace with webdrive wait
categories_container = categories_input.find_element_by_xpath('..')
categories = categories_container.find_elements_by_css_selector('li[class*="suggestion-list-item"]')
for category in categories:
if category.text == 'Professional Services':
category.click()
break
推荐阅读
- java - 如何使用对象作为映射键而不是 MongoDB Java 的字符串
- java - Install4j 安装组件问题
- angular - 频繁使用 BehaviorSubjects 会影响应用程序性能吗?
- scikit-learn - 为什么 onehotencoding 会将二进制数据转换为 2 个互斥的特征?
- datatables - 如何在 dataTables 中使用 columnDefs 和按钮?
- scikit-learn - sci-kit 的详细信息是否会发出一个信号,该信号可以被捕获并用于更新 PyQt 中的进度条
- android - 在 onResume() 之前动态更改 DialogFragment Max_Height?
- python - 如何通过用户输入动态更改站点中的数据框?(例如,可能按值重新排列同一列?从最大值到最小值?)
- cloud-foundry - PCF Credhub 服务代理 - 无法轮换凭据并要求重新启动
- groovy - 带有 lambda 的 java 8 字符串列表函数在 groovy 中不起作用