问题
I'm trying to add cookies to a link before I open it with webdriver but it keeps giving me this error:
org.openqa.selenium.UnableToSetCookieException: Unable to set cookie (WARNING: The server did not provide any stacktrace information)
Please find my code below:
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Please help with a relevant solution.
回答1:
You are creating the cookie before navigating to the site. If you are trying to create a cookie on the domain www.example.com, then you would want to navigate to some page on that domain, create the cookie, and then start your test.
From my reading a while back, the best way to do this is to navigate to some page you know will not exist on the domain, e.g. www.example.com/this404page, then create the cookie. It should load a lot faster since it's an error page and shouldn't contain much content. After creating the cookie on the 404 page, start your test.
回答2:
You are unable to do this because the WebDriver spec requires that you have the browser landed at the domain you are trying to set cookies for.
The work-around as mentioned is to go to the page prior to setting the cookie. But that causes some issues:
This unfortunately prevents 2 key use cases:
- You want to re-use cookies from another webdriver session in a new session to avoid repeating the work it took to get the cookies. Such as having to repeat a login.
- Allow a webdriver pool to be shared amongst unrelated threads where each thread might have their own cookies.
The webdriver spec clearly needs to take this into account. I have opened an issue here:
https://github.com/w3c/webdriver/issues/1238
Give it some votes. All browsers should start carrying a way to handle this.
回答3:
First navigate to URL and then try to add cookies, try below code:
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Replace your code with this :
System.setProperty("webdriver.edge.driver","C:\\Program Files\\Latest Webdriver\\MicrosoftWebDrive.exe" );
EdgeDriver = new EdgeDriver();
Thread.sleep(2000);
EdgeDriver.get("https://www.google.ca/?gws_rd=ssl"); // The link is an example
Cookie cookie = new Cookie("Testing", "11111");
EdgeDriver.manage().addCookie(cookie);
回答4:
Adding cookies before navigating to the domain are called domain-less cookies which i believe is not possible.
I have not found a way to drop a cookie before the url, but I think below scenario might help you out-
- Create a factory class to create webdriver instances
- Before returning the webdriver instance, navigating to any page on the domain under test, and drop the cookie, then navigate browser back.
- Your test can now begin, unaware that the navigation and cookie dropping has taken place
回答5:
Just in case this is simpler, I did not need to create a factory class, merely load the root page from the site (which did not demand a cookie)
First collect the cookie as described above
pickle.dump(driver.get_cookies(), open("ChromeCookies.pkl", "wb"))
Then get the web site root which does not require a cookie (in my case)
driver.get(url_root)
Then execute the cookie load
for cookie in pickle.load(open("ChromeCookies.pkl", "rb")):
driver.add_cookie(cookie)
Then go to the page that I actually wanted to access
driver.get(url_not_root_demanding_cookie_which_is_now_there)
Please do post if there is an issue with this approach
来源:https://stackoverflow.com/questions/45842709/unable-to-set-cookies-in-selenium-webdriver