gpt4 book ai didi

java - 在我的 session 中添加 cookie 时遇到问题

转载 作者:行者123 更新时间:2023-12-01 20:27:13 24 4
gpt4 key购买 nike

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

这是我的全类同学:

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");
}
}

最佳答案

因此,我自己解决了这个特殊问题,方法是不使用 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");
}

关于java - 在我的 session 中添加 cookie 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58909459/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com