Python selenium无法访问cookie

时间:2016-06-18 06:25:24

标签: python selenium cookies selenium-webdriver phantomjs

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

所以我尝试访问像:

这样的cookie
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设置它们?

1 个答案:

答案 0 :(得分:0)

默认情况下,PhantomJS启用了Cookie,因此我认为您所做的事情没有任何问题。

我认为问题在于亚马逊不支持“使用者”中的“Phantomjs /(..*)”。您是否尝试过使用此代码的其他网站?当你说你用FireFox尝试过它时,那是通过webdriver.Firefox()吗?

您可以尝试自定义useragent;

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() useragent&尝试将您的webdriver.PhantomJS()用户转换为Firefox使用的用户。