Extends Selenium WebDriver classes to include the request function from the Requests library, while doing all the needed cookie and request headers handling.
Before the actual request is made, a local HTTP server is started that serves a single request made by the webdriver instance to get the “standard” HTTP request headers sent by this webdriver; these are cached (only happens once during its lifetime) and later used in conjunction with the Requests library to make the requests look identical to those that would have been sent by the webdriver. Cookies held by the webdriver instance are added to the request headers and those returned in a response automatically set for the webdriver instance.
# Import any WebDriver class that you would usually import from # selenium.webdriver from the seleniumrequests module from seleniumrequests import Firefox # Simple usage with built-in WebDrivers: webdriver = Firefox() response = webdriver.request('GET', 'https://www.google.com/') print(response) # More complex usage, using a WebDriver from another Selenium-related module: from seleniumrequests.request import RequestMixin from someothermodule import CustomWebDriver class MyCustomWebDriver(CustomWebDriver, RequestMixin): pass custom_webdriver = MyCustomWebDriver() response = custom_webdriver.request('GET', 'https://www.google.com/') print(response)
pip install selenium-requests
The request method supports two additional arguments:
If the timeout is negative, then the script will be allowed to run indefinitely (similarly to Selenium WebDriver’s default behaviour).
The second is needed mostly because of PhantomJS. Every other webdriver blocks until the current window handle is loaded, except PhantomJS. If the new window handle was found via the first method (see above) and then cookies should be set, it’s possible that an exception is raised, because the webdriver has to have switched to a window handle with the correct domain prior to setting cookies. Thus the method waits page_load_timeout seconds before raising a TimeoutException.