0

Google Search Box Text Field

What is the fastest way to place a string into a text field using Python. Assuming the string can be parsed from an excel file

import webbrowser  
url = 'http://google.com'  
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open(url)

from pynput.keyboard import Key, Controller keyboard = Controller()

import xlrd


workbook = xlrd.open_workbook("Book1.xlsx") sheet =
workbook.sheet_by_index(0)

print (sheet.cell(0,0).value)
inkychris
  • 1,179
  • 2
  • 12
  • 18
  • Do you need the physical interaction with the UI of Chrome or do you just need to handle the response from a google search? `webbrowser` appears to be for redirecting users to a website in their browser where as if you don't need this, you may be better off with something like `robobrowser`. – inkychris Jul 15 '18 at 23:59

1 Answers1

2

webbrowser module does not offer you much control over your browser. The most direct and common way to approach your immediate problem is you use selenium library:

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://google.com")

search_input_box = driver.find_element_by_name("q")
search_input_box.send_keys("test search")

Make sure to follow the selenium installation steps and have chromedriver installed to be able to control Chrome browser through selenium.

Note that this is the direct answer to your question. You do not actually need to automate a regular browser to make a google search request:

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195