【问题标题】:Having problems adding a cookie to my session在我的会话中添加 cookie 时遇到问题
【发布时间】:2020-03-13 12:30:49
【问题描述】:

基本上是这样;我发现这 2 个类用于创建 cookie 并将其添加到某个网站的会话中,因为我对 cookie 一无所知。我从代码中删除了一些东西,比如“isSecured”,因为我认为这是不必要的。 创建 cookie 没问题,但将其添加到会话中不起作用,希望不是由于我删除的部分......但我真的认为它们并不重要。

这是我的全班:

package indeed;

import java.util.Scanner;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Cookies {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int input = 0;

        System.out.print("input 1 for readCookie or 2 for writeCookie: ");
        input = sc.nextInt();

        switch (input) {
            case 1:
                readCookie();
                break;
            case 2:
                writeCooke();
                break;
            default:
                System.out.println("no");
                break;
        }
    }

    public static void readCookie() {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        driver = new FirefoxDriver();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        driver.get("https://secure.indeed.com/account/login?hl=de&continue=%2Faccount%2Fview%3Fhl%3Dde");


        // Input Email id and Password If you are already Register
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email-input"))).sendKeys("example@email.com");
        driver.findElement(By.id("login-password-input")).sendKeys("examplepass");
        driver.findElement(By.id("login-submit-button")).click();

        // create file named Cookies to store Login Information
        File file = new File("Cookies.data");
        try {
            // Delete old file if exists
            file.delete();
            file.createNewFile();
            FileWriter fileWrite = new FileWriter(file);
            BufferedWriter Bwrite = new BufferedWriter(fileWrite);
            // loop for getting the cookie information

            // loop for getting the cookie information
            for (Cookie ck : driver.manage().getCookies()) {
                Bwrite.write((ck.getName() + ";" + ck.getValue()));
                Bwrite.newLine();
            }
            Bwrite.close();
            fileWrite.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void writeCooke() {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\geckodriver.exe");
        driver = new FirefoxDriver();
        try {

            File file = new File("Cookies.data");
            FileReader fileReader = new FileReader(file);
            BufferedReader Buffreader = new BufferedReader(fileReader);
            String strline;
            while ((strline = Buffreader.readLine()) != null) {
                StringTokenizer token = new StringTokenizer(strline, ";");
                while (token.hasMoreTokens()) {
                    String name = token.nextToken();
                    String value = token.nextToken();
                    String domain = token.nextToken();

                    Cookie ck = new Cookie(name, value, domain);
                    System.out.println(ck);
                    driver.manage().addCookie(ck); // This will add the stored cookie to your current session
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        driver.get("https://secure.indeed.com/account/login?hl=de&continue=%2Faccount%2Fview%3Fhl%3Dde");
    }
}

【问题讨论】:

    标签: java selenium cookies


    【解决方案1】:

    所以我自己解决了这个特殊问题,不使用 cookie,而是使用相同的 webdriver 实例来避免首先需要 cookie

    public static void switchToNewTab() {
        openNewTab();
        String subWindowHandler = null;
    
        Set<String> handles = getDriver().getWindowHandles();
        Iterator<String> iterator = handles.iterator();
        while (iterator.hasNext()) {
            subWindowHandler = iterator.next();
        }
        getDriver().switchTo().window(subWindowHandler);
    }
    
    public static void openNewTab() {
        ((JavascriptExecutor) getDriver()).executeScript("window.open('about:blank','_blank');");
    }
    
    public static void firstRunWebpage() {
        getDriver().get("https://employers.indeed.com/p#post-job");
        getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email-input"))).sendKeys("example@email.com");
        getDriver().findElement(By.id("login-password-input")).sendKeys("examplepass");
        //getDriver().findElement(By.xpath("//*[@id=\"label-login-rememberme-checkbox\"]")).click(); //typically already checked
        getDriver().findElement(By.id("login-submit-button")).click();
    }
    
    public static void afterFirstRun() {
        switchToNewTab();
        getDriver().get("https://employers.indeed.com/p#post-job");
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      • 2014-10-16
      • 2017-01-18
      • 2011-10-24
      相关资源
      最近更新 更多