1

Is there a way to open a browser from a Python application, while posting data to it in POST method?

I tried using webbrowser.open(url) to open the browser window, but I need to also post some data to it (via POST variables), to make sure the page opens with the proper information.

As an example, I would like to open http://duckduckgo.com, while posting "q=mysearchterm" as POST data to it, so the page will open with pre-filled data.

Tom Shir
  • 462
  • 1
  • 3
  • 14

1 Answers1

0

I believe you should be using a Web Driver such as Selenium. Here is an example:

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

browser = webdriver.Chrome(r"PATHTO\chromedriver.exe") # download the chromedriver and provide the full path here
browser.get('http://duckduckgo.com/')

search = browser.find_element_by_name('q')
search.send_keys("mysearchterm")
search.send_keys(Keys.RETURN)
time.sleep(15) 
Jen
  • 635
  • 5
  • 9
  • Thanks, but this code is part of a software plugin, so therefore I can't add any more drivers / components to the code. – Tom Shir Aug 02 '18 at 09:44
  • Just though of a way for your example to work but not sure what exactly you are trying to do. You can use string concatenation to get the search string url. For example, webbrowser.open( "http://duckduckgo.com"+"mysearchterm") – Jen Aug 02 '18 at 19:28