python - 无法单击搜索下拉框
问题描述
我正在尝试等待搜索下拉框出现,然后使用以下代码片段在https://amazon.com上单击它。
search_dropdown_box = WebDriverWait(chrome_browser,30).until(EC.visibility_of_element_located((By.ID,"searchDropdownBox")))
然而,尽管如此,代码片段似乎永远不会工作,它总是以失败告终,并出现以下异常。
File "C:/Users/DHIWAKAR-PC/PycharmProjects/AlationProject/assignment.py", line 18, in <module>
search_dropdown_box = WebDriverWait(chrome_browser,10).until(EC.visibility_of_element_located((By.ID,"searchDropdownBox")))
File "C:\Python34\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
我使用预期条件的方式有什么问题,还是我可以利用一些更好的预期条件?
解决方案
您正在尝试等待一个不可见且不可点击的元素,直到All
单击下拉菜单。我的意思是,您尝试单击的定位器在单击All
下拉菜单后将变为可见或可单击,并且您在此处使用了错误的定位器。
尝试使用//div[@id='nav-search-dropdown-card']/div
as xpath,这样您就可以识别All
下拉按钮并可以单击它。
如果要从下拉列表中选择选项,则需要searchDropdownBox
在单击All
下拉列表后使用 as id。
试试下面的代码:
driver.get('https://www.amazon.com/')
search_dropdown_box = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='nav-search-dropdown-card']/div")))
search_dropdown_box.click()
如果你想在点击后从下拉列表中选择任何选项All
,那么你可以使用下面的 python Select
:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
driver = webdriver.Chrome('chromedriver path')
driver.get('https://www.amazon.com/')
search_dropdown_box = WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='nav-search-dropdown-card']/div")))
search_dropdown_box.click()
options = driver.find_element_by_id('searchDropdownBox')
select = Select(options)
select.select_by_visible_text('Baby')
我希望它有帮助...
推荐阅读
- python - pip install bug with `-e` flag and `setuptools.setup(package_dir=...)` 参数?
- swagger - OpenAPI 3, 文件下载, content-type 事先不知道内容时
- html - 2 单击后打开引导下拉菜单
- ios - 出现键盘时调整 UITextView 的大小
- c# - Google Cloud API 从证书 (C#) 进行身份验证
- gradle - Jacoco 无法在 Gradle 构建中读取执行数据文件
- regex - 正则表达式查找最新的合并分支名称
- java - 将 Excel 数据转换为 HTML 图表
- opencv - cv2:去扭曲后不需要的裁剪(initUndistortRectifyMap)
- node.js - MongoClient - 在一台机器上工作,但不能在另一台机器上工作。如何输出配置/版本信息