首页 > 解决方案 > 如何为 Selenium 中遍历的每个页面设置唯一的 cookie?

问题描述

用例:

第 1 步:调用 Selenium 并遍历到第 2 页并退出

第二步:调用Sikuli在windows中执行一些动作并退出

第 3 步:现在调用 Selenium 并从第 2 页或重放停止的地方继续。

重播应该从第 2 页开始,但我总是从主页开始。那么有没有一种方法可以为在 Selenium 中遍历的每个页面创建和发送唯一的 cookie,以便以后使用。

我在退出第 2 页(第 1 步)之前获得了 cookie,并在第 3 步恢复它们。但它总是把我带到主页。

所以我正在寻找一个选项,我们是否可以为每个页面(第 2 页)创建唯一的 cookie,并使用它直接遍历第 2 页而不是主页。

伪代码如下:

//setting user-defined cookie
HttpCookie cookie = new HttpCookie(cookieName,cvalue);
          cookie.setDomain(domain);
          cookie.setPath(path);
          cookie.setMaxAge(3600);
          cookie.setSecure(flag);

Above is the cookie which I generated before exiting from Step 2

Sysout(cookie);

test=754895648; expires=Wed, 13 Jun 2029 04:53:44 IST; path=/; domain=vdevpril0.com;secure;


Passing below uniquely created cookie along with original cookie captured
  Cookie ck1 = new Cookie(name,value,domain,path,expiry,true);//auto captured cookie by selenium driver
  Cookie ck2 = new Cookie(name,value,domain,path,expiry,true);//user-defined cookie         
  driver.manage().addCookie(ck1); 
  driver.manage().addCookie(ck2);

如果我只通过 ck2,我将进入登录页面。ck1 帮助我自动登录和停止。

预期:第 3 步应该带我到第 2 页

实际:第 3 步进入主页

标签: javaseleniumselenium-webdrivercookies

解决方案


推荐阅读