简体   繁体   English

Python 硒测试。 如何从 Google 主页的搜索框中提取自动建议?

[英]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.我正在做一些自动测试 Python + Selenium。有没有办法在谷歌中检查建议框,例如使用 selenium。 Something like I would like to now that suggestion table is revealed when auto test put google in search bar.就像我现在想的那样,当自动测试将谷歌放在搜索栏中时,建议表会显示出来。

这是一个例子

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.使用 for 循环遍历所有元素,并在 WebElement 上调用文本

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 :要从Google 主页上的搜索框中提取自动建议,您必须使用expected_conditions作为visibility_of_all_elements_located诱导WebDriverWait ,如下所示:

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?在这里您可以找到有关如何自动化 Google 主页自动建议的相关讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 Python 从自动建议中选择 select - How to select an option from the auto suggestions using Selenium and Python 如何使用 Selenium (Python) 进行 Google 搜索,然后在新选项卡中打开第一页的结果? - How can I use Selenium (Python) to do a Google Search and then open the results of the first page in new tabs? 在搜索框中输入文本,然后从Selenium和Python自动完成中选择 - Inputting text in search box and then selecting from Auto Complete with Selenium and Python 我如何从图表硒Python中提取数据 - How can i extract data from a chart selenium python 如何使用 python + selenium 从 div 中提取内容 - How can I extract the content from a div using python + selenium 如何使用 python、selenium 和 chromedriver 从网站中提取此值 - how can i extract this value from a website, with python, selenium and chromedriver 通过谷歌硒搜索建议 - selenium search through google suggestions 如何使用Selenium Python从reddit.com搜索页面上的问题中提取标题和href属性 - How to extract the title and href attributes from the questions on reddit.com search page using Selenium Python 为什么需要从chrome浏览器打开google主页以输入来自硒的搜索 - why it is required to open google home page from chrome browser to enter search from selenium 如何使用硒python从自动完成框提取数据 - how to extract data from autocomplete box with selenium python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM