选择操作网页
我用的edge,谷歌我的版本太高没有对应的驱动…
下载Edge的驱动程序,直接解压就好里面只有一个.exe文件
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
复制即用,看注释
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;public static String getCookie(){//写你下载驱动程序的位置System.setProperty("webdriver.edge.driver", "D\\msedgedriver.exe");EdgeOptions options = new EdgeOptions();options.addArguments("--remote-allow-origins=*");EdgeDriver driver = new EdgeDriver(options);//打开网页driver.navigate().to("https://user/index.html");try {Thread.sleep(1000); // 停止一秒,单位是毫秒} catch (InterruptedException e) {e.printStackTrace();}// 找对应文件 点击driver.findElement(By.linkText("密码登录/注册")).click();try {Thread.sleep(1000); // 停止一秒,单位是毫秒} catch (InterruptedException e) {e.printStackTrace();}driver.findElement(By.id("username")).sendKeys("1564885");driver.findElement(By.id("password")).sendKeys("41556x");driver.findElement(By.linkText("登录")).click();try {Thread.sleep(1000); // 停止一秒,单位是毫秒} catch (InterruptedException e) {e.printStackTrace();}//登录后拿cookieWebDriver.Options manage = driver.manage();Set<Cookie> cookies = manage.getCookies();String cookie = "";for(Cookie c : cookies) {System.out.println(c.getName() + " - "+c.getValue());}//关闭页面,看需求//driver.close();System.out.println(cookie);}