如何通过Python使用Selenium填充部分文本后如何单击自动完成功能?

问题描述 投票:0回答:2

我正在测试表单,它具有类别字段,它是基于输入的下拉菜单。用.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 Webdriver。

谢谢。

python selenium selenium-webdriver autocomplete webdriverwait
2个回答
0
投票

这不是一个很好的解决方案,但我会进行如下编码以从下拉列表中选择项目。

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

0
投票

要单击文本为专业服务>软件开发]的选项,您必须为element_to_be_clickable()引入WebDriverWait,并且可以使用以下任何一种Locator Strategies

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    driver.get("https://biz.yelp.com/signup_business/new")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='categories']"))).send_keys("Software Development")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='categories']//following::div[1]/ul/li"))).click()
    
  • 浏览器快照:

  • dropdown

© www.soinside.com 2019 - 2024. All rights reserved.