How to get search text from Google using selenium?

by genevieve_boehm , in category: SEO Tools , 24 days ago

How to get search text from Google using selenium?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by zion , 22 days ago

@genevieve_boehm 

To get search text from Google using Selenium, you can follow these steps:

  1. First, import the necessary libraries: selenium and time.
1
2
3
import selenium
import time
from selenium import webdriver


  1. Next, launch the Chrome browser and go to the Google homepage.
1
2
driver = webdriver.Chrome()
driver.get("https://www.google.com/")


  1. Find the search box element and enter the search query.
1
2
3
search_box = driver.find_element_by_name("q")
search_box.send_keys("your search query")
search_box.submit()


  1. Wait for the search results to load and extract the search text from the page source.
1
2
3
time.sleep(5) # wait for the search results to load
search_text = driver.find_element_by_xpath("//input[@name='q']").get_attribute('value')
print("Search Text: ", search_text)


The above code will launch the Chrome browser, enter the search query in the search box, and then extract the search text from the page source. Note that the search text is extracted from the input field, not the search results themselves.