seleniumbase / SeleniumBase

📊 Python's all-in-one framework for web crawling, scraping, testing, and reporting. Supports pytest. UC Mode provides stealth. Includes many tools.
https://seleniumbase.io
MIT License
5.16k stars 958 forks source link

Requesting Advice on Modifying Specific Cookie Values and Reload #2652

Closed LiPingYen closed 6 months ago

LiPingYen commented 6 months ago

I'm currently working on a web automation testing project where I've utilized sb.save_cookies('cookies.txt') to store cookies. My objective is to modify certain values within these cookies and then reload them using sb.load_cookies('cookies.txt') for execution on a website.

Could anyone provide suggestions or best practices on how to efficiently modify specific element values within these cookies before reloading them onto the website?

mdmintz commented 6 months ago

If you save cookies using sb.save_cookies("cookies.txt"), you might get a file that looks something like this in ./saved_cookies/cookies.txt:

[{"domain": "www.saucedemo.com", "expiry": 1712082307, "httpOnly": false, "name": "session-username", "path": "/", "sameSite": "Lax", "secure": false, "value": "standard_user"}]

If you run:

sb.open("https://www.saucedemo.com")
sb.load_cookies("cookies.txt")
sb.open("https://www.saucedemo.com/inventory.html")

then you have bypassed the need to login because you already loaded the login cookie.

If you need to need to modify the cookies before loading them, that's just Python file-IO, and that isn't a SeleniumBase-specific question anymore. Lots of resources online, such as on Stack Overflow. However, I may add a method in the next release for getting the cookies from that file without loading them into the browser right away. Could simplify a few things.

LiPingYen commented 6 months ago

Hi Michael,

I can't wait for the new feature for adding cookies separately. After some searching, I think my question is about data wrangling not just for cookies and I can solve it by myself.

mdmintz commented 6 months ago

I added more cookie methods in 4.25.1 - https://github.com/seleniumbase/SeleniumBase/releases/tag/v4.25.1