iNoob
iNoob

Reputation: 1395

Pythons requests session to open browser using selenium

Im looking to use requests.session and beautifulsoup. If a specific status of 503 is identified I want to then open that session in a web browser. The problem is I have no idea how to move a python requests session into a browser using selenium. Any guidance would be appreciated.

Upvotes: 0

Views: 3487

Answers (1)

Mark Ignacio
Mark Ignacio

Reputation: 431

Requests sessions have CookieJar objects that you can use to import into Selenium.

For example:

driver = webdriver.Firefox()
s = requests.Session()
s.get('http://example.com')

for cookie in s.cookies:
    driver.add_cookie({
        'name': cookie.name, 
        'value': cookie.value,
        'path': '/',
        'domain': cookie.domain,
    })

driver should now have all of the cookies (and therefore sessions) that Requests has.

Upvotes: 2

Related Questions