- 1、登录博客园(要勾选记住我)
-
2、如图所示获取对应的cookie
image.png - 3、完整代码如下
package com.test;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
public class Dome {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "f://chromedriver//chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(20000);
driver.get("https://www.cnblogs.com/");
//cookie遍历使用
Set<Cookie> cookies = driver.manage().getCookies();
for(Cookie cookie : cookies) {
System.out.println(cookie.getName()+"\t"+cookie.getValue());
}
Cookie cookie = new Cookie("key", "value");
//添加cookie
driver.manage().addCookie(cookie);
//Cookie cookie1 = new Cookie(".CNBlogsCookie", "9A7DE89257E3765C3246D960635FD370F9E73A85EB4B9ABA71EA3BAC4622104880E78E51BBB135926C27D1181919FED9071F9B49F97B12F4D4B5AB503B12232DFA65D1C8A9C7C88F9C4DCBAB923AFAEAF1EB1920");
//driver.manage().addCookie(cookie);
//Cookie cookie2 = new Cookie(".Cnblogs.AspNetCore.Cookies", "CfDJ8J0rgDI0eRtJkfTEZKR_e804cteVpQc-bau8PRfg15hLzElMcHWQ8D33898dZmXaOEwEf4ieZi6vtqHR5ULwGP4xupO7dAT-89PBoct8P7y3qcBGnEQ8mtQLZeRmZUQZzudGFEoIzcDghsjn3OH6Jl6cUXp_QfHb4iPVZeRwvECBRIsOtr6jMFT-VAB93UgFd69SeS2pET6tfeudlIKHoFT29-FKv0LkNqMbzTtlllJ73Gt3CvNY3pYSlP8CN-K4QGVJn7EpU9jaF--6YvSgsInNGeV7QRR_xHJ8XWwzXVSQmjd8B0ZWOcbRCziH9fNAmA");
Cookie cookie1 = new Cookie("key", "value");
driver.manage().addCookie(cookie1);
Cookie cookie2 = new Cookie("key", "value");
driver.manage().addCookie(cookie2);
Cookie cookie3 = new Cookie("key", "value");
driver.manage().addCookie(cookie3);
driver.manage().window().maximize();
driver.navigate().refresh();
}
}
网友评论