1

I want to collect a series of responses when navigating a website, and afterwards "recreate" the process using the responses.

From an other thread I found this solution to render HTML:

content = requests.get("https://stackoverflow.com/").content

driver = webdriver.Chrome()
driver.get("data:text/html;charset=utf-8," + content)

Unfortunately, when I try this (using Firefox instead of Chrome), the content is simply put into the browser address bar.

How can I render a series of responses, including for example XHR-responses with selenium webdriver?

Community
  • 1
  • 1
Peter1807
  • 184
  • 9

2 Answers2

1

You have to account for certain browser-specific things, like the fact that # and % have to be escaped if you use Firefox - from what I understand, you can simply pass the content through quote():

try:
    from urllib import quote  
except ImportError: 
    from urllib.parse import quote # if Python 3

driver.get("data:text/html;charset=utf-8," + quote(content))

No need to do that if you use Chrome.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thank you! But can I also process all other requests that way? Just as if the browser would send requests, and would receive dummy-responses - without having to send requests. I want to feed responses, and see the browsers behavior. – Peter1807 Sep 24 '16 at 14:15
  • 1
    @Peter1807 not sure, could you please elaborate on what are you trying to achieve and why do you need a real browser instead of, say, using just `requests`? Thanks. – alecxe Sep 24 '16 at 14:41
  • I want to test client-side frameworks. The server responses contain templates in those cases, that are only rendered in the browser. I want to see how altering these templates affects the resulting page. – Peter1807 Sep 24 '16 at 15:00
  • 1
    @Peter1807 okay, but can you use `driver.get()` to get to the templates directly, without intermediate `requests.get()`? – alecxe Sep 24 '16 at 15:04
  • I would collect the responses from another source. So I would not need to use requests.get() – Peter1807 Sep 24 '16 at 15:34
0

I found a possible solution, or rather workaround. When saving requests (urls) and responses in a dictionary, you can set up a server that answers each request with its resprective pre-recorded response.

Peter1807
  • 184
  • 9