简体   繁体   中英

Adding Cookies using Selenium Ghostdriver PhantomJS with python

I have been using firefox + selenium for scraping. However, i decided to switch to Phantomjs as it should be faster.

I am now having problem adding cookies when using Phantomjs + ghostdriver + selenium with python. To solve the problem, i have been searching online for solution but i cannot find the right solution.

Below is the code used.

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')

Error message gotten:

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"}}

The main error is "Can only set Cookies for the current domain". However, i have already navigated to the website before adding the cookies. I have also noticed that the host in the request header is 127.0.0.1 which is local host. This can be the source of the problem. Is there any way to solve this? How can i change the host in the request header? Thanks in advance.

when adding cookie in step:

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

you should use key 'domain' with low-case 'd' instead of 'Domain'

when adding cookie, 'domain' is required. This is my example,

Including add cookie , get cookie and delete 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM