Java Selenium Chromedriver 加载 Cookie,但不使用它们



我需要每天多次访问一个网站,并希望跳过登录页面。这就是为什么我想在Java Selenium Chromedriver中使用Cookie,以便在一天中第一次访问它后跳过该登录。Selenium正在正确保存cookie,但不使用它们,我无法访问下一页。你可以帮我吗?

这是我的代码:

public static void main(String[] args) throws InterruptedException {
    Set<Cookie> cookie = null;
    Iterator<Cookie> itr = null;
    while (true) {
        System.setProperty("webdriver.chrome.driver", "C:\Users\Maxi\Desktop\ChromeDriver.exe");
        driver = new ChromeDriver();
        driver.get("https://www.xxxxxx.xxx");
        while (itr != null && itr.hasNext()) {
            driver.manage().addCookie(itr.next());
        }
        driver.navigate().refresh();
        WebDriverWait wait0 = new WebDriverWait(driver, 20);
        if (itr == null) {
            String UserID = "LoginFieldXpath";
            wait0.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(UserID)));
            driver.findElement(By.xpath(UserID)).sendKeys("Username");
            String PW = "PasswordField Xpath";
            driver.findElement(By.xpath(PW)).sendKeys("Password");
            String LogIn = "LoginButtonXpath";
            driver.findElement(By.xpath(LogIn)).click();
            cookie = driver.manage().getCookies();
            itr = cookie.iterator();    
        }
    }
}

您可以在 Chrome 中创建这样的用户个人资料选项:

addArguments("user-data-dir="+"path_to_empty_folder");

然后进行注册,因此cookie将存储在此配置文件中。而不仅仅是将此用户数据目录复制到另一个文件夹

FileUtils.copyDirectory(new File("path-to-dir-with-cookie"), new File("new-dir"));
options.addArguments("user-data-dir="+"new-dir");

Selenium每次都会启动一个新的临时浏览器实例,因此它不会存储任何cookie,缓存或类似的东西。

我建议将打开的 chrome 驱动程序移动到 while 循环之外,这样您就可以在每次想要检查页面时重复使用它。也许也可以将登录名移到循环之外,或者检查是否需要登录。然后,每次需要时,只需抓住您尝试在循环内检查的任何页面。

public static void main(String[] args) throws InterruptedException {
    Set<Cookie> cookie = null;
    Iterator<Cookie> itr = null;
    // move this outside the loop
    System.setProperty("webdriver.chrome.driver", "C:\Users\Maxi\Desktop\ChromeDriver.exe");
    driver = new ChromeDriver();
    while (true) {
        driver.get("https://www.xxxxxx.xxx");
        // not sure why you're adding a cookie?
        // it should automatically accept page cookies that are set
        while (itr != null && itr.hasNext()) {
            driver.manage().addCookie(itr.next());
        }
        driver.navigate().refresh();
        WebDriverWait wait0 = new WebDriverWait(driver, 20);
        // check to make sure that you need to log in
        if (itr == null) {
            String UserID = "LoginFieldXpath";
            wait0.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(UserID)));
            driver.findElement(By.xpath(UserID)).sendKeys("Username");
            String PW = "PasswordField Xpath";
            driver.findElement(By.xpath(PW)).sendKeys("Password");
            String LogIn = "LoginButtonXpath";
            driver.findElement(By.xpath(LogIn)).click();
            cookie = driver.manage().getCookies();
            itr = cookie.iterator();    
        }
    }
}

最新更新