2

I am having trouble setting cookies in my python selenium browser. I have a front-end that runs on http://localhost:9999/ It is an Angular 6 application that makes REST calls to http://localhost:8888/ In order to make these REST calls to http://localhost:8888/, I need to set up a cookie for the domain http://localhost:8888/. This is my code.

from psb_framework.seleniums.model import SharedDriver

if __name__ == "__main__":
    shared_driver = SharedDriver()
    shared_driver.driver = SharedDriver.start_new(browser="chrome")

    #set the cookie domain
    shared_driver.driver.get('http://localhost:8888/') #backend

    my_cookie = {"name": "cname", "value": "cvalue", "path": "/", "secure": False}

    shared_driver.driver.delete_all_cookies()
    shared_driver.driver.add_cookie(my_cookie)
    shared_driver.driver.refresh()

    #this line causes the cookie domain to change to http://localhost:9999/
    #  which is not what I want
    shared_driver.driver.get('http://localhost:9999/') #frontend

The problem I'm having is that, while I can see the cookie in the browser, one I make the GET call to http://localhost:9999/ in the last line, the cookie domain is suddenly http://localhost:9999/ instead of http://localhost:8888/

I found similar questions here and here but I think they only work because all of the requests are on the same domain and port. My requests use different ports.

What am I doing wrong?

EDIT: After playing around more, I think the problem may be that my Angular/Typescript code isn't using the cookie when sending the REST call. This is how I'm making the REST call:

result = this.http.get<any>("http://localhost:9999/", { withCredentials: true }).pipe(
  ...
);
user2023861
  • 8,030
  • 9
  • 57
  • 86

0 Answers0