首页 > 解决方案 > 填充部分文本后如何单击自动完成?

问题描述

我正在测试一个表单,它有一个类别字段,它是一个基于输入的下拉菜单。使用 .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 网络驱动程序。

谢谢。

标签: pythonseleniumselenium-webdriverautocomplete

解决方案


这不是一个优雅的解决方案,但我会编写类似下面的代码来从下拉列表中选择项目。

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

推荐阅读