首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python selenium无法访问cookies

Python selenium无法访问cookies
EN

Stack Overflow用户
提问于 2016-06-18 14:25:24
回答 1查看 1.1K关注 0票数 2

我正在尝试使用python、selenium和phantomjs访问网页。我使用driver.get获得页面,并登录到页面。它说我应该为此启用cookie。

因此,我尝试访问cookies,如下所示:

代码语言:javascript
复制
self.driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any'])
self.driver.get('https://sellercentral.amazon.in/gp/homepage.html')

cks = self.driver.get_cookies()
for cookie in cks:
    print cookie
    print cookie['name'] + ' - ' + cookie['value']

但是我在终端里看到的只是一个空的列表。但当我使用mozilla firefox访问该页面时,会为该域设置多个cookie。

如何访问这些cookie并为webdriver设置它们?

EN

回答 1

Stack Overflow用户

发布于 2016-06-18 18:34:03

默认情况下,PhantomJS会启用Cookie,所以我不认为您所做的事情有任何问题。

我认为问题出在亚马逊没有在用户代理中支持"Phantomjs/(..*)“。你有没有尝试过其他网站的代码?你说你已经在FireFox上尝试过了,是通过webdriver.Firefox()吗?

您可以尝试自定义用户代理;

代码语言:javascript
复制
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap["phantomjs.page.settings.userAgent"] = (
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1"
)
driver = webdriver.PhantomJS(desired_capabilities=dcap)

用于此的Docs

但是您应该尝试通过webdriver.Firefox()运行它,看看它是否工作,然后可能读取webdriver.Firefox()用户代理,并尝试将您的webdriver.PhantomJS()用户代理切换到火狐所使用的用户代理。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37893959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档