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 ?

6
  • 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? Commented Apr 22, 2016 at 6: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 Commented Apr 25, 2016 at 7:47
  • 1
    Why are you opening in a browser? Commented Apr 25, 2016 at 9: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. Commented Apr 25, 2016 at 15:22
  • Have you tried selenium-requests? Commented May 2, 2016 at 5:50

1 Answer 1

14
+25

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.