1

i'm trying to login to a website with the requests library and use the session from the login afterwards for selenium because there are some elements are loaded dynamically. I dont know how i get hand over the request-session to selenium. Or is there a way to get the dynamic content without selenium?

I tried to save the cookies from teh request-session in a .pkl file and load them again with selenium, but that doesn't work because the : "Object of type 'Cookie' ist not JSON serializable" This entry doesnt help me out: https://stackoverflow.com/questions/37499452/python-requests-cookies-export-session-to-selenium

s = requests.session()      
r = s.post(urlForSignin, data=payload)

with open(file,'wb') as f:
    pickle.dump(s.cookies, f)

driver = webdriver.Firefox()
driver.delete_all_cookies()
cookies = pickle.load(open(file", "rb"))

for cookie in cookies:
     driver.add_cookie(cookie)
     driver.get(urlForSignin)

I expect that i can work in the request-session from the login in selenium. Am i wrong?

Ollum_mc
  • 23
  • 1
  • 4
  • 2
    Why don't you use Selenium to login directly? – TYZ Apr 17 '19 at 18:51
  • I thought it would be better to avoid Selenium because my internet connection is very bad and therefore the site needs a lot to load – Ollum_mc Apr 17 '19 at 18:54
  • what I usually do in that case is to wait for a couple seconds (or longer) until I perform the next action. If you have a good sense of how long it takes for the website to load, you can add a `sleep`. – TYZ Apr 17 '19 at 18:56
  • Is there any way to secure that the page is loaded without ```sleep````? – Ollum_mc Apr 17 '19 at 18:59
  • Here are some options (https://blog.codeship.com/get-selenium-to-wait-for-page-load/), but they work similar as having a `sleep`, just uses selenium's capability – TYZ Apr 17 '19 at 19:05
  • Allright, i will try that. Thank you! – Ollum_mc Apr 17 '19 at 19:06
  • As noted in the edit on the top answer for [this question](https://stackoverflow.com/questions/26566799/wait-until-page-is-loaded-with-selenium-webdriver-for-python), selenium's built-in capability to wait for a page to load will not fully wait for ajax calls, but will wait for most other elements to load in – C.Nivs Apr 17 '19 at 19:07

0 Answers0