我正在测试一个表单,它有一个类别字段,它是一个基于输入的下拉菜单。使用 .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
关于python - 填写部分文本后如何点击自动完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60073607/