简体   繁体   English

无法单击搜索下拉框

[英]Unable to click a search drop down box

I'm trying to wait for the search drop down box to appear before clicking on it on https://amazon.com using the following snippet of code. 我正在尝试等待搜索下拉框出现,然后使用以下代码片段在https://amazon.com上单击它。

search_dropdown_box = WebDriverWait(chrome_browser,30).until(EC.visibility_of_element_located((By.ID,"searchDropdownBox")))

Despite this however, the snippet of code never seems to work, it always ends up failing with the following exception. 尽管如此,这段代码似乎永远无法正常工作,但始终会因以下异常而失败。

  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: 

Is there something wrong with how I'm using the expected condition or is there some better expected condition that I can make use of ? 我使用预期条件的方式是否有问题,还是可以利用更好的预期条件?

You are trying to wait for an element that is invisible and not clickable until the All drop down is clicked. 您试图等待一个不可见且不可单击的元素,直到单击“ All下拉列表。 I mean, the locator that you are trying to click will become visible or clickable after clicking on the All drop down and you are using the wrong locator here. 我的意思是,您要单击的定位器在单击All下拉列表后将变为可见或可单击,并且您在此处使用了错误的定位器。

Try to use //div[@id='nav-search-dropdown-card']/div as xpath, so that you can identify the All drop down button and can click on it. 尝试使用//div[@id='nav-search-dropdown-card']/div作为xpath,以便您可以标识All下拉按钮并单击它。

If you want to select options from the drop down then you need to use searchDropdownBox as id after clicking on the All drop down. 如果要从下拉列表中选择选项,则需要在单击All下拉列表后使用searchDropdownBox作为ID。

Try the below code: 试试下面的代码:

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()

If you want to select any option from the drop down after clicking on All , then you can use the python's Select like below: 如果您要在单击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')

I hope it helps... 希望对您有帮助...

I think you can try to use element_to_be_clickable 我认为您可以尝试使用element_to_be_clickable

search_dropdown_box = WebDriverWait(chrome_browser,30).until(EC.element_to_be_clickable((By.ID,"searchDropdownBox")))

See https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions 请参阅https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.support.expected_conditions

I faced the same problem in Java, which was solved using the fluent wait. 我在Java中也遇到了同样的问题,这可以通过流畅的等待解决。

For pyton, refer to this link: Java's FluentWait in Python 对于pyton,请参考以下链接: Python中的Java FluentWait

you will need to add TimeoutException in ignored_exceptions list. 您将需要在ignore_exceptions列表中添加TimeoutException。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM