gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 07:34:33 26 4
gpt4 key购买 nike

我需要每天多次访问某个网站,并且想要跳过登录页面。这就是为什么我想在 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");

关于Java Selenium Chromedriver加载Cookie但不使用它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35584006/

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