Render HTTP Response (HTML content) in selenium webdriver (browser)

I use the Requests module to send GET and POST requests to websites and then process their responses. If Response.text meets certain criteria, I want it to be open in a browser. To do this, I am currently using the selenium package and forwarding the request to the web page through the selenium web server. However, I feel that this is inefficient, since I already received the response once, so is there a way to make this received Response object directly in the browser that opens through selenium?

EDIT A hacker way I could think of is to write Response.text to a temporary file and open it in a browser. Please let me know if there is a better way to do this than this?

+6
source share
1 answer

To directly display some HTML with Selenium, you can use the data scheme with the get method:

 from selenium import webdriver import requests content = requests.get("http://stackoverflow.com/").content driver = webdriver.Chrome() driver.get("data:text/html;charset=utf-8," + content) 

Or you can write the page using a script fragment:

 from selenium import webdriver import requests content = requests.get("http://stackoverflow.com/").content driver = webdriver.Chrome() driver.execute_script(""" document.location = 'about:blank'; document.open(); document.write(arguments[0]); document.close(); """, content) 
+5
source

All Articles