[英]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.