• Java selenium对cookies的操作


    public static void main(String[] args) {
    
        ChromeDriver driver = new ChromeDriver();
        
        driver.get("http://www.youdao.com/");
        
        //获取cookies
        Set<Cookie> data = driver.manage().getCookies();
        System.out.println(data);
        //获得cookies个数
        System.out.println(driver.manage().getCookies().size());
        
        //删除所有cookies
        driver.manage().deleteAllCookies();
        //获得cookies个数
        System.out.println(driver.manage().getCookies().size());//此时cookies的个数为0
        
        //设置cookies
        Cookie c1 = new Cookie("JSESSIONID", "A93C195B41FE5252CFB1AFFB381037F2");
        driver.manage().addCookie(c1);
        System.out.println(driver.manage().getCookies().size());//新增了一条,此时cookie个数为1
    
        driver.close();
        
    }

     实例二 参考:

    https://blog.csdn.net/wangh92/article/details/105995060
    package debug;
    
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.openqa.selenium.*;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.springframework.util.StringUtils;
    
    import java.util.HashSet;
    import java.util.Random;
    import java.util.Set;
    import java.util.concurrent.ConcurrentHashMap;
    
    public class test {
        public static ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
    
        public static void main(String[] args) {
            System.err.println(getCookie("13100000000", "221.122.91.74:9401"));
        }
    
        public static String getCode(String mobile) {
            //平台获取验证码接口
            return "349587";
        }
    
        @SuppressWarnings("deprecation")
        public static String getCookie(String mobile, String proxyIpAndPort) {
            //chromedriver必须和安装的谷歌浏览器版本一致
             System.setProperty("webdriver.chrome.driver", "F:\java\workspace\crawler\chromedriver.exe");
            //System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");  //chromederiver存放位置
        //System.setProperty("webdriver.chrome.bin", "/opt/google/chrome/chrome");  //chrome安装位置
            ChromeOptions chromeOptions = new ChromeOptions();
            if (StringUtils.isEmpty(proxyIpAndPort)) {
                chromeOptions.addArguments("--headless", "no-sandbox", "--disable-gpu", "--start-maximized");
            } else {
                chromeOptions.addArguments("--headless", "no-sandbox", "--disable-gpu", "--start-maximized", "--proxy-server=" + proxyIpAndPort);
            }
            WebDriver driver = new ChromeDriver(chromeOptions);
            String getCookie = null;
            //请求部分
            try {
                
                
                driver.get("http://ehall.tjut.edu.cn/publicapp/sys/zxzxapp/index.do#/consultingList");
                driver.findElement(By.id("username")).sendKeys("信息门户账号");        //登陆界面填写手机号
                Thread.sleep(new Random().nextInt(100) + 200);
                driver.findElement(By.id("password")).sendKeys("我的密码");        //登陆界面填写手机号
                Thread.sleep(new Random().nextInt(100) + 200);
                driver.findElement(By.className("submitBtn")).click();        //点击手机登陆按钮
                Document document = Jsoup.parse(driver.getPageSource());
    //            driver.findElement(By.className("bh-headerBar-roleBox-title")).click();    //获取到登陆按钮
    //            driver.findElement(By.id("20180807000451660")).click();    //获取到登陆按钮
    
                System.out.println(document);
                
                
    //            driver.get("http://m.yangkeduo.com/login.html");
    //            driver.findElement(By.className("phone-login")).click();        //点击手机登陆按钮
    //            Thread.sleep(new Random().nextInt(100) + 200);
    //            driver.findElement(By.id("user-mobile")).sendKeys(mobile);        //登陆界面填写手机号
    //            Thread.sleep(new Random().nextInt(100) + 200);
    //            driver.findElement(By.id("code-button")).click();                //点击发送验证码按钮
    //            Thread.sleep(new Random().nextInt(50) + 99);
    //            driver.findElement(By.id("input-code")).sendKeys(getCode(mobile));     //将验证码填入
    //            Thread.sleep(new Random().nextInt(100) + 200);
    //            WebElement element = driver.findElement(By.id("submit-button"));    //获取到登陆按钮    
    //            element.sendKeys(Keys.ENTER);                                        //对登陆按钮敲击回车
                Thread.sleep(new Random().nextInt(100) + 900);
                Set<Cookie> cookies = driver.manage().getCookies();                    //获取cookies
                Set<String> set = new HashSet<>();
              //处理cookies[_nano_fp=XpEol09xX5gYlpPbXC_bZb339IF0A6JcsUSlYehG; expires=星期五, 01 五月 2048 04:49:09 CST; path=/; domain=m.yangkeduo.com, api_uid=CklG8V/YeIQjNwBYOHB7Ag==; expires=星期五, 01 一月 2038 07:55:55 CST; path=/; domain=.yangkeduo.com, pdd_vds=gaALjIVLHOMPqNpiXbzmKbpaVmgPWoWPXNqLpEJoJGjIqypyqQpbjOqQFLgI; expires=星期二, 22 十二月 2020 04:49:08 CST; path=/; domain=m.yangkeduo.com, webp=1; path=/; domain=m.yangkeduo.com] api_uid=CklG8V/YeIQjNwBYOHB7Ag==
    
                System.out.println(cookies);                                                                    
    //            for (Cookie cookie : cookies) {
    //                String[] regex = cookie.toString().split(";");
    //                for (String key : regex) {
    //                    String[] split = key.split("=");
    //                    if (split[0].equalsIgnoreCase("PDDAccessToken") || split[0].equalsIgnoreCase("api_uid")) {
    //                        set.add(key);
    //                    }
    //                }
    //            }
    //            getCookie = String.join(";", set);
    //            map.put(mobile, getCookie);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
    //            driver.close();
            }
            return getCookie;
        }
    
    }
    
    //参考链接:https://blog.csdn.net/wangh92/article/details/105995060
  • 相关阅读:
    CCF CSP-J/S2020第二轮认证评级名单
    CSP-J/S2020第二轮提高组北京市获奖分析
    CCF CSP-J/S2020第二轮评级规则
    [ZZ]80% 的信息学竞赛生都不知道的 USACO!含金量极高
    初步估分体验
    【ZZ】近3年北京市科技特长生招生趋势分析,编程好的孩子有哪些优势?
    立冬--2020CSP-J 游记
    P1061 Jam的计数法
    P1476 休息中的小呆
    P1444 [USACO1.3]虫洞wormhole
  • 原文地址:https://www.cnblogs.com/StarZhai/p/14137950.html
Copyright © 2020-2023  润新知