Python selenium Can';无法访问cookies
我正在尝试使用python、selenium和phantomjs访问一个网页。我使用Python selenium Can';无法访问cookies,python,selenium,cookies,selenium-webdriver,phantomjs,Python,Selenium,Cookies,Selenium Webdriver,Phantomjs,我正在尝试使用python、selenium和phantomjs访问一个网页。我使用驱动程序获取页面。获取并登录页面。它说我应该为此启用cookies 所以我尝试访问cookies,比如: self.driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=any']) self.driver.get('https://sellercentral.amazon.in/gp/home
驱动程序获取页面。获取并登录页面。它说我应该为此启用cookies
所以我尝试访问cookies,比如:
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设置它们?默认情况下,PhantomJS会启用cookie,因此我认为您的操作没有任何问题
我认为问题在于Amazon在useragent中不支持“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)
为了这个
但是你应该尝试通过webdriver.Firefox()
来运行它,看看它是否有效,然后潜在地阅读webdriver.Firefox()
useragent&尝试将你的webdriver.PhantomJS()
useragent切换到Firefox使用的代理。不,我没有从webdriver使用Firefox。我使用浏览器firefox并在其控制台中检查cookies。如何在amazon上使用phantonjs因为我所有的脚本都在phantomjs中,所以我更愿意在amazon上使用它。我可以在使用phantomjs和selenium时更改用户代理吗?@ManishGupta您应该尝试webdriver.Firefox()
,如果只是为了证明亚马逊在嗅探用户代理时就是这么做的。我在windowsMozilla/5.0(X11;Ubuntu;Linux x86_64;rv:47.0)Gecko/20100101 Firefox/40.1
上使用了这个用户代理,但在Linux上却没有。然后我使用了我的linux用户代理Mozilla/5.0(X11;Ubuntu;linux x86_64;rv:47.0)Gecko/20100101 Firefox/47.0
,它仍然无法工作。我需要让它在linux中工作,因为我将使用ubuntu服务器进行脚本部署。@ManishGupta您在Firefox上试用过吗?那么,至少你可以确定,问题是亚马逊不会向PhantomJS提供cookie?我现在已经提供了,它向我抛出了“如何修复Selenium WebDriverException:浏览器似乎在我们连接之前就退出了?”