如何在python硒浏览器中为其他端口设置cookie?

时间:2018-07-20 15:48:39

标签: python-3.x typescript selenium angular6

我在python硒浏览器中设置cookie时遇到问题。我有一个在http://localhost:9999/上运行的前端,这是一个Angular 6应用程序,它对http://localhost:8888/进行REST调用为了对http://localhost:8888/进行这些REST调用,我需要设置一个域http://localhost:8888/的Cookie。这是我的代码。

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

我遇到的问题是,虽然我可以在浏览器中看到cookie,但我在最后一行对http://localhost:9999/进行了GET调用,但cookie域突然变成了http://localhost:9999/http://localhost:8888/

我发现了类似的问题herehere,但我认为它们只起作用,因为所有请求都在相同的域和端口上。我的请求使用其他端口。

我在做什么错了?

编辑:玩了很多之后,我认为问题可能是我的Angular / Typescript代码在发送REST调用时未使用cookie。这就是我进行REST调用的方式:

result = this.http.get<any>("http://localhost:9999/", { withCredentials: true }).pipe(
  ...
);

0 个答案:

没有答案
相关问题