phantomjs --version.I use selenium php-webdriver to set cookie on chrome and phantomjs.
On chrome, it is all right.
But on phantomjs, I got the exception:Can only set Cookies for the current domain, which is located at:https://github.com/ariya/phantomjs/blob/master/src/ghostdriver/request_handlers/session_request_handler.js#L752
I have read this issue:https://github.com/facebook/php-webdriver/issues/294, and I am sure that the $driver had finished loading current url.

I also explicitly set the cookie domain to current url but failed.
This question:http://stackoverflow.com/questions/30292025/ on stackoverflow is highly related but also failed to solve my problem.
Could you give me some idea?
It seems the problem here
@gouchaoer that is part of the webdriver specification. ghostdriver is doing the right thing.
I think it just does not do the right thing because the spec says:
Cookie domain: (...) Defaults to the current browsing context鈥檚 document鈥檚 URL domain if omitted when adding a cookie.
So if it would default to the current domain's browsing context it would work, but in PJS is never works if the domain is not explicitly set, no matter if I was at that domain before or not. All other drivers I tested (HtmlUnit, Chrome, IE, Edge, Firefox) habndle this situation gracefully, setting the cookie for the current domain, onls PJS does not. This is clearly a regression with regards to WebDriver standard, user expectations and other WebDriver implementations.
As soon as I explicitly set the cookie domain manually, it works. So it is a matter of wrong default behaviour.
@kriegaex the issue with setting the default domain should be fixed in the next version of ghostdriver.
see detro/ghostdriver#498. this fix is included in my pull request to phantomjs and ghostdriver
Good news, thanks. But is Ghostdriver still actively developed? The read-me on its font page suggests it is not. Or does the PJS crew now also maintain Ghostdriver? The separation vs. connection of the two projects and who does what has never been clear to me as a mere user. I think this needs clarification on both projects' front pages or FAQs.
Meanwhile the workaround of manually setting the cookie domain works, it is just ugly.
But is Ghostdriver still actively developed?
yes, i am actively fixing bugs in ghostdriver for the phantomjs 2.5.0 release. i plan on fixing more bugs and adding w3c webdriver features after the current release.
Or does the PJS crew now also maintain Ghostdriver?
pjs crew does not maintain ghostdriver.
The separation vs. connection of the two projects and who does what has never been clear to me as a mere user.
fair point i'll try to make this clearer.
Thank for your feedback, I understand it a lot better now. :-)
Hi,
I hit this bug (or a related one) as well, using phantomjs 2.1.1 on Linux 64-bit. I'm unable to set cookies at all using Ruby watir-webdriver (which in turn uses selenium-webdriver).
There also seems to be erratic behavior. Setting a cookie providing the domain explicitly work if and only if I have previously attempted (and failed) to set the cookie without the domain.
Test code I'm running:
Kernel.puts "----------- START -----------"
@browser.goto("http://localhost.localdomain.test:8080/web/empty")
Kernel.puts "TITLE: #{@browser.title}"
Kernel.puts "URL: #{@browser.url}"
Kernel.puts "\n @browser.cookies.add without domain:"
begin
@browser.cookies.add("foo", "bar")
Kernel.puts "*** WORKED ***"
rescue => e
Kernel.puts " #{e}"
end
Kernel.puts "\n @browser.cookies.add with domain:"
begin
@browser.cookies.add("foo", "bar", domain: "localhost.localdomain.test")
Kernel.puts "\n*** WORKED ***\n"
rescue => e
Kernel.puts " #{e}"
end
Kernel.puts "\n @browser.driver.manage.add_cookie without domain:"
begin
@browser.driver.manage.add_cookie(name: "foo", value: "bar")
Kernel.puts "\n*** WORKED ***\n"
rescue => e
Kernel.puts " #{e}"
end
Kernel.puts "\n @browser.driver.manage.add_cookie with domain:"
begin
@browser.driver.manage.add_cookie(name: "foo", value: "bar", domain: "localhost.localdomain.test")
Kernel.puts "\n*** WORKED ***\n"
rescue => e
Kernel.puts " #{e}"
end
Kernel.puts "-------------- DONE -----------"
The output of this is:
````
----------- START -----------
TITLE: This page is intentionally left blank
URL: http://localhost.localdomain.test:8080/web/empty
@browser.cookies.add without domain:
{"errorMessage":"Can only set Cookies for the current domain","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"79","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{"cookie":{"name":"foo","value":"bar","secure":false,"path":"/","domain":null}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/86383bb0-0fbc-11e7-8f51-b9290e16e9d6/cookie"}} (org.openqa.selenium.InvalidCookieDomainException)
@browser.cookies.add with domain:
* WORKED *
@browser.driver.manage.add_cookie without domain:
{"errorMessage":"Can only set Cookies for the current domain","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"65","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{"cookie":{"name":"foo","value":"bar","path":"/","secure":false}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/86383bb0-0fbc-11e7-8f51-b9290e16e9d6/cookie"}} (org.openqa.selenium.InvalidCookieDomainException)
@browser.driver.manage.add_cookie with domain:
* WORKED *
-------------- DONE -----------
````
If I comment out the first and third test, then the second and fourth test start failing as well:
````
----------- START -----------
TITLE: This page is intentionally left blank
URL: http://localhost.localdomain.test:8080/web/empty
@browser.cookies.add with domain:
{"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"103","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{"cookie":{"name":"foo","value":"bar","secure":false,"path":"/","domain":"localhost.localdomain.test"}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/9c95c9e0-0fbc-11e7-8bf2-85b621562abf/cookie"}} (org.openqa.selenium.UnableToSetCookieException)
@browser.driver.manage.add_cookie with domain:
{"errorMessage":"Unable to set Cookie","request":{"headers":{"Accept":"application/json","Accept-Encoding":"gzip;q=1.0,deflate;q=0.6,identity;q=0.3","Connection":"close","Content-Length":"103","Content-Type":"application/json; charset=utf-8","Host":"127.0.0.1:8910","User-Agent":"Ruby"},"httpVersion":"1.1","method":"POST","post":"{"cookie":{"name":"foo","value":"bar","domain":"localhost.localdomain.test","path":"/","secure":false}}","url":"/cookie","urlParsed":{"anchor":"","query":"","file":"cookie","directory":"/","path":"/cookie","relative":"/cookie","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/cookie","queryKey":{},"chunks":["cookie"]},"urlOriginal":"/session/9c95c9e0-0fbc-11e7-8bf2-85b621562abf/cookie"}} (org.openqa.selenium.UnableToSetCookieException)
-------------- DONE -----------
````
As a workaround I'm now doing the following:
begin
@browser.cookies.add(name, value)
rescue => e
# Due to a bug, the line above fails on phantomjs 2.1.1
# The following works *after* the line above has been executed
@browser.cookies.add(name, value, domain: $env.domain)
end
@plaa this should be fixed in the next version of phantomjs.
Yes, @plaa, this is the usual workaround. I do the same when using PJS via Ghostdriver from Spock/Geb (Groovy) or JUnit/Selenium (Java).
I just wrote a python version of the ruby workaround above, in case anyone wants it
self.browser.get(self.live_server_url)
try:
self.browser.add_cookie({
'name': 'sessionid',
'value': cookie.value,
'secure': False,
'path': '/',
})
except:
self.browser.add_cookie({
'name': 'sessionid',
'value': cookie.value,
'secure': False,
'path': '/',
'domain': 'localhost',
})
Ohh, I have some problem like that! At C# i use that method ExecuteScript(), and write the following code at JS for that method 'document.cookie = "MyCookieName = MyCookieValue" '
Due to our very limited maintenance capacity (see #14541 for more details), we need to prioritize our development focus on other tasks. Therefore, this issue will be automatically closed. In the future, if we see the need to attend to this issue again, then it will be reopened. Thank you for your contribution!
Most helpful comment
I just wrote a python version of the ruby workaround above, in case anyone wants it