简体   繁体   English

将requests.Session()cookie转移到Python中的selenium Web驱动程序

[英]Transferring requests.Session() cookies to a selenium web driver in Python

After researching and tinkering, I seem to be stumped as to what I could try. 在研究和修补之后,我似乎对我能尝试的东西感到难过。 I'm essentially looking to do the reverse of this question right here: Is it possible to "transfer" a session between selenium.webdriver and requests.session 我基本上是想在这里做相反的问题: 是否可以在selenium.webdriver和requests.session之间“转移”一个会话

I want to "click" on a JavaScript button on a webpage that I've "reached" through a series of GET/POST requests in a session (it's important that the cookies are maintained and seamlessly transferred since my GET/POST requests are on pages that require a logged-in user). 我希望通过会话中的一系列GET / POST请求“点击”我已经“到达”的网页上的JavaScript按钮(由于我的GET / POST请求已启用,因此保持并无缝传输cookie非常重要需要登录用户的页面。

However, after some googling, I found that requests doesn't seem to offer something like that. 然而,经过一些谷歌搜索后,我发现请求似乎没有提供类似的东西。 I found selenium and have since been trying to properly transfer the cookies over (unsuccessfully). 我发现了硒,并且一直试图将饼干正确转移(失败)。

import requests, requests.utils, lxml.html
from lxml.cssselect import CSSSelector
from selenium import webdriver

# urls which requests will be made to
login_url = 'login-url-here'
logged_in_data_url = 'logged-in-data-here'

# create my Session to contain my cookies
with requests.Session() as s:
    login_html = s.get(login_url)
    tree = lxml.html.fromstring(login_html.text)
    important_key1 = list(set(tree.xpath('//*[@id="fm1"]/div/div[3]/input[1]/@value')))[0]
    important_key2 = list(set(tree.xpath('//*[@id="fm1"]/div/div[3]/input[2]/@value')))[0]
    form_value = "submit"

    login_payload = {
        'post-field-1': 'post-data-1',
        'post-field-2': 'post-data-2',
        'important_key1': 'important_value1',
        'important_key2': 'important_value2',
        'important_key3': 'important_value3'
    }

    login_result = s.post(login_url,
                    data=login_payload,
                    headers = dict(referer=login_url))

    logged_in_data_html = s.get(logged_in_data_url)
    tree = lxml.html.fromstring(logged_in_data_html.text)
    print(logged_in_data_html.text)

    # Attempt at transferring cookies, currently fails
    cookie_dict = requests.utils.dict_from_cookiejar(s.cookies)
    driver = webdriver.Firefox()
    for cookie in cookie_dict:
        driver.add_cookie(cookie)

    driver.get(logged_in_data_url)

    # prints same contents as login_html.text,
    # meaning cookie transfer failed and the session was thrown out
    print(driver.page_source) 

Any advice or pointers on what to do from here? 有什么建议或指示从这里做什么?

EDIT: My attempt with selenium-requests : 编辑:我对selenium-requests尝试:

import seleniumrequests
import lxml.html
from lxml.cssselect import CSSSelector

# urls which requests will be made to
login_url = 'login-url-here'
logged_in_data_url = 'logged-in-data-here'

driver = seleniumrequests.Firefox()

login_html = driver.request('GET', login_url)
tree = lxml.html.fromstring(login_html.text)
important_key1 = list(set(tree.xpath('//*[@id="fm1"]/div/div[3]/input[1]/@value')))[0]
important_key2 = list(set(tree.xpath('//*[@id="fm1"]/div/div[3]/input[2]/@value')))[0]
form_value = "submit"

# following print statements print value1, value2 respec
print ("important_key1 = " + important_key1)
print("important_key2 = " + important_key2)

login_payload = {
    'post-field-1': 'post-data-1',
    'post-field-2': 'post-data-2',
    'important_key1': 'important_value1',
    'important_key2': 'important_value2',
    'important_key3': 'important_value3'
}

login_result = driver.request('POST', login_url,
                              data=login_payload,
                              headers = dict(referer=login_url))

# this should print out the landing page after being logged in
# source code contains important_key1, 2, and 3 with different values
# the GET and POST requests seem to be in different sessions
# how do I fix that?
print(login_result.text)

I don't believe it is possible to do that natively. 我不相信它可以原生那样做。 There is, however, an extension to Selenium called selenium-requests that you should be able to use. 但是,Selenium的扩展称为硒请求 ,您应该能够使用它。

EDIT: 编辑:

Try adding the following to your code. 尝试在代码中添加以下内容。 Upon reading the source, this should work (and use the requests Session auto-initialized during the POST request. 在阅读源代码时,这应该可以工作(并使用POST请求期间自动初始化的requests Session

response = driver.request('GET', logged_in_data_url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM