selenium 模拟登陆 并获取登陆后的cookie等信息 java

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
package com.example.demo.util;

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

    public static String getCookie(String mobile, String proxyIpAndPort) {
        //chromedriver必须和安装的谷歌浏览器版本一致
         System.setProperty("webdriver.chrome.driver", "D:/chromeDriver/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://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();
            Set<String> set = new HashSet<>();
            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;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值