10

I am using Requests module to send GET and POST requests to websites and then processing their responses. If the Response.text meets a certain criteria, I want it to be opened up in a browser. To do so currently I am using selenium package and resending the request to the webpage via the selenium webdriver. However, I feel it's inefficient as I have already obtained the response once, so is there a way to render this obtained Response object directly into the browser opened via selenium ?

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

bawejakunal
  • 1,678
  • 2
  • 25
  • 54
  • short answer no. Long answer, there are hacky ways like what you are trying but why bother? What do you gain by all that effort? – e4c5 Apr 22 '16 at 06:01
  • @e4c5 as I said, I want to open a page response in selenium only if it meets a certain set of conditions in its response, if I simply open up all the requests in browser that would make my application slower due to the unnecessary responses that the browser will be rendering – bawejakunal Apr 25 '16 at 07:47
  • 1
    Why are you opening in a browser? – Padraic Cunningham Apr 25 '16 at 09:33
  • @PadraicCunningham to check for XSS execution, the code tries to inject alerts as a proof-of-concept RXSS and then checks in the browser for it successful execution. – bawejakunal Apr 25 '16 at 15:22
  • Have you tried [`selenium-requests`](https://pypi.python.org/pypi/selenium-requests/)? – JRodDynamite May 02 '16 at 05:50
  • @JRodDynamite yes I tried `selenium-requests` but that doesn't seem to be working as expected, there isn't proper rendering within the browser, moreover it opens and closes new tabs instead of working in the current tab. – bawejakunal May 03 '16 at 04:22

1 Answers1

14

To directly render 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 could write the page with a piece of script:

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)
Florent B.
  • 41,537
  • 7
  • 86
  • 101