Alex Nikitin
Alex Nikitin

Reputation: 941

Python Selenium Testing. How can I extract the Auto Suggestions from search box on Google Home Page?

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

Upvotes: 0

Views: 2183

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193268

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?

Upvotes: 0

Naveen Kumar R B
Naveen Kumar R B

Reputation: 6398

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.

Upvotes: 2

Related Questions