通过python使用Selenium Ghostdriver PhantomJS添加cookie

通过python使用Selenium Ghostdriver PhantomJS添加cookie,python,selenium,cookies,phantomjs,ghostdriver,Python,Selenium,Cookies,Phantomjs,Ghostdriver,我一直在使用firefox+selenium进行抓取。然而,我决定切换到Phantomjs,因为它应该更快 我现在在python中使用Phantomjs+ghostdriver+selenium时添加cookie时遇到问题。为了解决这个问题,我一直在网上寻找解决方案,但我找不到正确的解决方案 下面是使用的代码 from selenium import webdriver from selenium.webdriver.common.by import By from selenium.commo

我一直在使用firefox+selenium进行抓取。然而,我决定切换到Phantomjs,因为它应该更快

我现在在python中使用Phantomjs+ghostdriver+selenium时添加cookie时遇到问题。为了解决这个问题,我一直在网上寻找解决方案,但我找不到正确的解决方案

下面是使用的代码

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

driver = webdriver.PhantomJS()
driver.get("http://kswarrants.kasikornsecurities.com/www/Tool/calculator")   
driver.add_cookie({'name':'Disc', 'value':'YES','Domain':'kswarrants.kasikornsecurities.com'})
options = driver.find_elements_by_xpath('//select[@id="underling0"]/option')
获取错误消息:

selenium.common.exceptions.WebDriverException: Message: {"errorMessage":"Can only set Cookies for the current domain","request":{"headers":{"Accept":"applicatio
n/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"110","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1","User-
Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"2eb47d00-fdb0-11e5-af40-4fb0a42a2c0b\", \"cookie\": {\"path\": \"/\",
\"name\": \"Disc\", \"value\": \"YES\"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cook
ie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"
/session/2eb47d00-fdb0-11e5-af40-4fb0a42a2c0b/cookie"}}

主要错误是“只能为当前域设置Cookies”。但是,在添加cookie之前,我已经导航到了该网站。我还注意到请求头中的主机是127.0.0.1,它是本地主机。这可能是问题的根源。有什么办法解决这个问题吗?如何更改请求标头中的主机?提前感谢。

在步骤中添加cookie时:

driver.add_cookie({'name':'Disc', 'value':'YES','Domain':'kswarrants.kasikornsecurities.com'})

在步骤中添加cookie时,应使用小写字母“d”的键“domain”,而不是“domain”:

driver.add_cookie({'name':'Disc', 'value':'YES','Domain':'kswarrants.kasikornsecurities.com'})

添加cookie时,应使用小写字母“d”的键“domain”,而不是“domain”

需要“domain”。这是我的例子,

包括添加cookie获取cookie删除cookie

require 'selenium-webdriver'
require 'phantomjs'

# webdriver.PhantomJS
# install gems and phantomJs first
Selenium::WebDriver::PhantomJS.path = 'C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.get 'http://stackoverflow.com/'
puts driver.title

# additional keys that can be passed in are:
# :path => String, :secure -> Boolean, :expires -> Time, DateTime, or seconds since epoch
driver.manage.all_cookies.each { |cookie|
  puts "#{cookie[:name]} => #{cookie[:value]}, #{cookie[:domain]}"
}

# add cookie !! domain is required !!
driver.manage.add_cookie(:name => 'key', :value => 'value', :domain => '.stackoverflow.com')
# Delete By name
driver.manage.delete_cookie 'key'
# Or delete all of them
driver.manage.delete_all_cookies
driver.quit

添加cookie时,需要“域”。这是我的例子,

包括添加cookie获取cookie删除cookie

require 'selenium-webdriver'
require 'phantomjs'

# webdriver.PhantomJS
# install gems and phantomJs first
Selenium::WebDriver::PhantomJS.path = 'C:\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe'
driver = Selenium::WebDriver.for :phantomjs
driver.get 'http://stackoverflow.com/'
puts driver.title

# additional keys that can be passed in are:
# :path => String, :secure -> Boolean, :expires -> Time, DateTime, or seconds since epoch
driver.manage.all_cookies.each { |cookie|
  puts "#{cookie[:name]} => #{cookie[:value]}, #{cookie[:domain]}"
}

# add cookie !! domain is required !!
driver.manage.add_cookie(:name => 'key', :value => 'value', :domain => '.stackoverflow.com')
# Delete By name
driver.manage.delete_cookie 'key'
# Or delete all of them
driver.manage.delete_all_cookies
driver.quit