0

I like the ease of filling out and submitting online forms using RoboBrowser and I think I understand how to access the requests.Session() instance underlying RoboBrowser if I need to use that.

But I want to submit a form using RoboBrowser then pass the session to requests_html.Session() so I can render the HTML using JavaScript. How do I do that? Is there a way to convert a Requests session to a Requests-HTML session?

I have looked through the documentation for Requests, Requests-HTML and RoboBrowser, as well as through all SO questions about Requests-HTML. I have also Googled for the answer. None of these sources helped.

I am aware that it might be easier to use Selenium for this purpose, but this is for a project at work, where I cannot install Selenium. I believe my broader question of how to convert or pass a Requests session to a Requests-HTML session is a useful one for the Python community.

Mez
  • 21
  • 6

1 Answers1

2

I found the answer in the Requests-HTML source code. There is a specific class method called HTMLResponse._from_response() for this purpose, which takes a response as its first argument and a session as its second argument.

Say we have a robobrowser.RoboBrowser() object named browser. Then the underlying requests.Response() object is accessible by browser.response. To pass this to a requests_html.HTMLSession() called session, do as follows:

import requests_html

html_response = requests_html.HTMLResponse._from_response(browser.response, session)

html_response.html.render()  # This now works
Mez
  • 21
  • 6