0

I am doing some auto testing Python + Selenium.Is there any way to check suggestion box in google for example using selenium. Something like I would like to now that suggestion table is revealed when auto test put google in search bar.

Here is an axample

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alex Nikitin
  • 841
  • 1
  • 10
  • 29
  • 1
    There is a div with class 'gstl_0 sbdd_a' which contains these suggestions as a series of li elements inside it. These li contain the text of each suggestion – Grasshopper Mar 16 '18 at 16:59

2 Answers2

2

try the following code:

suggestions = driver.find_elements_by_css_selector("li[class='sbsb_c gsfs']")
for element in suggestions:
    print(element.text)

Iterate through all elements using for loop, and call text on WebElement.

Naveen Kumar R B
  • 6,248
  • 5
  • 32
  • 65
0

To extract the Auto Suggestions from Search Box on Google Home Page you have to induce WebDriverWait with expected_conditions as visibility_of_all_elements_located as follows :

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

driver = webdriver.Chrome(executable_path="C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.get("http://www.google.com")
search_field = driver.find_element_by_name("q")
search_field.send_keys("google")
searchText_google_suggestion = WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//form[@action='/search' and @role='search']//ul[@role='listbox']//li//span")))
for item in searchText_google_suggestion :
    print(item.text)

Console Output :

google
google translate
google maps
google drive
google pixel 2
google earth
google news
google scholar
google play store
google photos

Here you can find a relevant discussion on How to automate Google Home Page auto suggestion?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352