3
经过研究和修补之后,我似乎被困在了我可以尝试的地方。我基本上想在这里做这个问题的相反:Is it possible to "transfer" a session between selenium.webdriver and requests.session将requests.Session()cookie传递给Python中的selenium web驱动程序
我想“点击”一个网页上的JavaScript按钮,我通过一系列的GET/POST请求会话(由于我的GET/POST请求位于需要登录用户的页面上,Cookie保持并无缝传输非常重要)。
但是,在一些谷歌搜索后,我发现请求似乎没有提供这样的东西。我发现硒,一直试图正确地转移曲奇(失败)。
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)
有什么建议或指示从这里做什么?
编辑:我尝试用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)
我曾尝试用硒的要求,但我遇到了一个单独的问题,我将履行以下:1)通过seleniumrequests.Firefox()创建一个webdriver; 2)在login_url上发出GET请求; 3)执行xpath报废以获得即将到来的POST的必要数据; 4)尝试使用数据进行POST; 5)从驱动中读取page_source(并且它仍然会读取与login_url页面相同的源代码,这意味着它尚未登录。我想我可以再次尝试它... –
如果您将代码作为* *更新**到你原来的帖子,我可能会有帮助 – 2Cubed
我很抱歉等了很长时间 - 我已经将我的尝试/代码添加到了原帖(并且更具体地描述了(希望)我的问题是硒-requests)! –