Reputation:
import webbrowser, sys, time
import pyautogui
print("Googling...")
googleSearch = "+".join(sys.argv[1:])
if len(sys.argv) > 1:
webbrowser.open_new_tab("https://google.com/?q=%s"%googleSearch)
time.sleep(3)
print("Enter button is pressed")
pyautogui.typewrite(["enter"])
else:
webbrowser.open_new_tab("https://www.google.com/")
This is a basic program to open a google search through the command prompt. Sometimes, the page takes a while to load and thus the enter button will be pressed before it's fully loaded. Using time.sleep()
works most times when the loading takes a while but is there a more efficient way to do this? I know selenium
has some explicit wait method but selenium takes a little long to open a page and it's a little impractical to open a google search on a new browser.
Upvotes: 6
Views: 6769
Reputation: 971
Maybe this help.
I use the logo to detect if the website is already loaded. But could be anything, a button, etc.
url = 'https://site'
webbrowser.open(url)
while True:
logo = pyautogui.locateOnScreen('logo.png')
if logo is not None:
break
print logo
Upvotes: 1
Reputation: 37539
You could use the google search
endpoint, so you don't need to click enter in the search box on the main page of google
safe_search = urllib.parse.quote_plus(search_term)
webbrowser.open_new_tab("https://google.com/search?q={}".format(safe_search)
Upvotes: 1