Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为网站添加和使用cookies?_Java_Html_Selenium_Cookies - Fatal编程技术网

Java 如何为网站添加和使用cookies?

Java 如何为网站添加和使用cookies?,java,html,selenium,cookies,Java,Html,Selenium,Cookies,我正在开发一个用于发布招聘信息的自动化软件。 当我通过电子邮件和密码登录时,它告诉我在运行4次后填写验证码,所以我想使用cookies,但我无法让它工作 我看了几段视频,但在遇到以下代码之前,大多数视频我都不理解: File f = new File("browser.data"); try{ f.delete(); f.createNewFile(); FileWriter fos = new FileWriter(f);

我正在开发一个用于发布招聘信息的自动化软件。 当我通过电子邮件和密码登录时,它告诉我在运行4次后填写验证码,所以我想使用cookies,但我无法让它工作

我看了几段视频,但在遇到以下代码之前,大多数视频我都不理解:

    File f = new File("browser.data");
    try{
        f.delete();
        f.createNewFile();
        FileWriter fos = new FileWriter(f);
        BufferedWriter bos = new BufferedWriter(fos);

        for(Cookie ck: driver.manage().getCookies()) {
            bos.write((ck.getName()+";"
            +ck.getValue()+";"
            +ck.getDomain()+";"
            +ck.getPath()+";"
            +ck.getExpiry()+";"
            +ck.isSecure()));
            System.out.println(ck);
            bos.newLine();
        }
        bos.flush();
        bos.close();
        fos.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
这对创建cookie很好,但当我尝试使用它时,什么都没有发生

这是我所有的主要工作,我是一个真正的noob当谈到硒和饼干等,所以我希望有人在这里可以帮助我

public static void main(String[] args) {
System.setProperty("webdriver.firefox.driver","C:\\geckodriver.exe");
FirefoxDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 10);

    driver.get("examplejobpostingsite.com");
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("login-email-input"))).sendKeys("test@protonmail.com"); //let cookies take over later on
    driver.findElement(By.id("login-password-input")).sendKeys("pass");
    driver.findElement(By.id("login-submit-button")).click();

    wait.equals(2);

    File f = new File("browser.data");
    try{
        f.delete();
        f.createNewFile();
        FileWriter fos = new FileWriter(f);
        BufferedWriter bos = new BufferedWriter(fos);

        for(Cookie ck: driver.manage().getCookies()) {
            bos.write((ck.getName()+";"
            +ck.getValue()+";"
            +ck.getDomain()+";"
            +ck.getPath()+";"
            +ck.getExpiry()+";"
            +ck.isSecure()));
            System.out.println(ck);
            bos.newLine();
        }
        bos.flush();
        bos.close();
        fos.close();
    }catch(Exception ex){
        ex.printStackTrace();
    }
}
当我尝试
driver.manage().getCookieNamed(“browser.data”)

什么也没发生,除了10秒后我因为我的WebDriverWait而超时。

也许这会有帮助:可能的重复我创建cookie没有问题,但是我如何在代码中使用已经创建的cookie自动登录网站?或者,如果cookie中存储的域是网站的域,程序是否会自动执行此操作?如果插入的cookie没有自动登录您,则表示服务器端未接受cookie。在关闭浏览器一段时间后,检查cookie是否仍然有效。基本上你需要知道发布cookies的规则。