0

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 ?

Dhiwakar Ravikumar
  • 1,983
  • 2
  • 21
  • 36

3 Answers3

2

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

Ali
  • 1,689
  • 1
  • 5
  • 12
  • Couldn't have explained it better :), so the searchDropDownBox is basically an identifier for the drop down list, which isn't visible by default when the page is loaded ? – Dhiwakar Ravikumar Feb 11 '19 at 10:37
0

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

Robin Ding
  • 741
  • 6
  • 9
0

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.

Tejas Jagtap
  • 11
  • 1
  • 3