Home > Mobile >  Not able to select and click dropdown search query result with Selenium webdriver
Not able to select and click dropdown search query result with Selenium webdriver

Time:09-26

Im trying to perform select and click action from the search box result dropdown for testing purpose. Though i dont get ant error but i'm stuck and not able to do so, search results came then disappeared immediately. Please any one help me out. Im using Python script to automate webdriver. Here is the screenshot below for reference. enter image description here. I have tried webdriverwait for same action but it gives Timeout exception error. If there is any child actions from CSS to perform let me know. Here is what i tried

search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")

dropdown = WebDriverWait(driver, 4).until(
        EC.presence_of_element_located((By.XPATH, "//li[text()='flowers']")))

Apart from this snippet, i want to rather just perform enter key operation, where i get query result for 'flower' on this ecomm. website. Here is the website URL- https://paytmmall.com

CodePudding user response:

The suggested options are not containing the text directly in li elements, they are inside child elements inside li elements.
Try this instead:

search = driver.find_element_by_id('searchInput')
search.send_keys("flowers")

dropdown = WebDriverWait(driver, 4).until(
EC.visibility_of_element_located((By.XPATH, "//li//*[text()='flowers']")))

CodePudding user response:

Once you type flower in the input field, there are multiple options appearing based on the input provided. They are in li tags and under b tag.

Code :

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
#driver.implicitly_wait(30)
wait = WebDriverWait(driver, 30)
driver.get("https://paytmmall.com/")
search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
wait.until(EC.visibility_of_element_located((By.XPATH, "(//li)[4]/descendant::b[contains(text(),'flowers')]"))).click()

time.sleep is just for visibility purpose. you can remove that as well.

Also this xpath (//li)[4]/descendant::b[contains(text(),'flowers')] is based on xpath indexing , since I think you wanna select the 4th option which is flower itself. In case you wanna select a different option, you would have to write the different xpath.

In case you are looking to just select the searched item, it's better to pass enter key once you type flower in the input field.

You can use the below code for that :

search = wait.until(EC.visibility_of_element_located((By.ID, "searchInput")))
search.send_keys("flowers")
time.sleep(3)
search.send_keys(Keys.RETURN)
  • Related