Unable to set cookies in Selenium Webdriver

后端 未结 5 975
名媛妹妹
名媛妹妹 2020-12-09 16:45

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

5条回答
  •  离开以前
    2020-12-09 17:35

    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:

    1. 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.
    2. 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.

提交回复
热议问题