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

亡梦爱人 提交于 2019-12-11 05:04:13

问题


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.


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/49325855/python-selenium-testing-how-can-i-extract-the-auto-suggestions-from-search-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!