gpt4 book ai didi

java - selenium webdriver 如何为每次运行维护单独的 session ?

转载 作者:行者123 更新时间:2023-12-02 07:01:34 27 4
gpt4 key购买 nike

例如在gmail登录中,当我们考虑登录测试时,第一次手动执行时我们会进入登录页面,从下次开始我们将直接进入收件箱页面。

如果您尝试在 webdriver 中执行相同的操作(运行登录测试两次),在所有这些尝试中,我们将获得登录页面,因为我们之前没有从这台计算机登录。维护 cookie 或 session 的 session 的幕后发生了什么?

最佳答案

这是来自 selenium 文档的描述和代码片段,用于添加或删除 cookie:

Before we leave these next steps, you may be interested in understanding how to use cookies. First of all, you need to be on the domain that the cookie will be valid for. If you are trying to preset cookies before you start interacting with a site and your homepage is large / takes a while to load an alternative is to find a smaller page on the site, typically the 404 page is small (http://example.com/some404page)

// Go to the correct domain
driver.get("http://www.example.com");

// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);

// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}

// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();

关于java - selenium webdriver 如何为每次运行维护单独的 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16606433/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com