有没有办法在不结束会话的情况下退出selenium驱动程序? (蟒蛇)

时间:2017-03-03 01:09:52

标签: python selenium session selenium-webdriver python-requests

我正在使用PhantomJs webdriver登录网页(为了内存目的)我将会话cookie复制到我随后使用的requests.Session对象中。

现在,我想完全摆脱selenium.webdriver对象,因为它消耗了太多内存,从那时起它实际上没用。

但是,在我调用driver.quit()后,与服务器的连接似乎已关闭,我的请求对象不再登录。

伪代码:

from selenium import webdriver
import requests

driver = webdriver.PhantomJS()
driver.get(login_page)  # Do all the stuff here to login

client = requests.Session()
client.cookies.update({ck['name']: ck['value'] for ck in driver.get_cookies()})  # copy cookies

client.get(page_that_I_am_logged_in)  # works fine

driver.quit()  # quit the driver

client.get(page_that_I_am_logged_in)  # doesn't work anymore

1 个答案:

答案 0 :(得分:0)

要回答我自己的问题,似乎问题不是selenium驱动程序关闭连接,而是Session对象没有保持打开状态。

添加以下标题可解决问题

client.headers.update({'Connection':'Keep-Alive'})