相关疑难解决方法(0)

与另一个域的Selenium cookie

我有一个关于selenium的代码来测试表单.但首先我转到另一个页面,然后重定向到我的页面.当我将cookie设置为新域时,我收到错误:

Exception in thread "main" org.openqa.selenium.InvalidCookieDomainException: You may only set cookies for the current domain
Run Code Online (Sandbox Code Playgroud)

我的代码:

//it is going to example.com and example redirect me to the "example.com" all cookie domains is "example.com"
driver.get("http://www.example.com?id=1");

 Set<Cookie> cookies = driver.manage().getCookies();
 Iterator<Cookie> itr = cookies.iterator();

    while (itr.hasNext()){
    Cookie c = itr.next();
    System.out.println("Cookie Name: " + c.getName() + " --- " + "Cookie Domain: " + c.getDomain() + " --- " + "Cookie Value: " + c.getValue());

    driver.manage().addCookie(c);
    }
Run Code Online (Sandbox Code Playgroud)

我该如何管理?我必须为example.com获取/设置cookie

java cookies selenium

8
推荐指数
2
解决办法
1万
查看次数

保持用户登录 - 使用网络驱动程序保存cookie

这就是我想要使用WebDriver执行的操作.

  1. 使用选中的复选框登录该站点,保持用户登录.
  2. 关闭浏览器.
  3. 再次访问该站点并确保该用户已登录.

问题是当我关闭驱动程序并重新打开它时,我没有登录.

我的代码看起来像这样 -

   WebDriver myD = null;
    myD = new FirefoxDriver();
    String URL = "https://www.eurostylelighting.com/protected/account/signin?ReturnUrl=%2f";
    myD.navigate().to(URL);
    myD.findElement(By.cssSelector("input#txtEmailorRewards")).sendKeys("abc@yahoo.com");           
    myD.findElement(By.cssSelector("input#txtPassword")).sendKeys("abc");
    myD.findElement(By.xpath(".//*[@id='accountSignIn']/dl[4]/dd/label/span")).click();
Set<Cookie> cookies = myD.manage().getCookies();
    myD.close();
    myD= new FirefoxDriver();
    myD.navigate().to(URL);
for(Cookie getCookie:cookies)
            myD.manage().addCookie(getCookie);
Run Code Online (Sandbox Code Playgroud)

java selenium webdriver selenium-webdriver

3
推荐指数
1
解决办法
7717
查看次数

标签 统计

java ×2

selenium ×2

cookies ×1

selenium-webdriver ×1

webdriver ×1