简体   繁体   中英

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.

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. 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.

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.

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.

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:

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

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

I faced the same problem in Java, which was solved using the fluent wait.

For pyton, refer to this link: Java's FluentWait in Python

you will need to add TimeoutException in ignored_exceptions list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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