selenium-java依赖包下载
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
下载chromeDriver
注意:chromeDriver与chrome版本有关,如果版本不一致会发生错误。
下载chromeDriver:
http://chromedriver.storage.googleapis.com/index.html
https://getwebdriver.com/chromedriver#stable
下载时一定要看清版本
下载EdgeDriver
https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
使用selenium-java模拟登录csdn伪代码
public static void chrome() throws InterruptedException{
System.setProperty("webdriver.chrome.driver", "D:\\webdriver\\chromedriver.exe");
ChromeDriverService service = ChromeDriverService.createDefaultService();
ChromeOptions options = new ChromeOptions();
//options.setHeadless(true);
ChromeDriver webDriver = new ChromeDriver(service,options);
webDriver.get("https://mp.csdn.net");
TimeUnit.SECONDS.sleep(1);
String cookieValue = "uuid_tt_dd=10_30613105130-1714273041354-134792;.......";
// ((JavascriptExecutor) webDriver).executeScript("document.cookie='" + cookieValue+"'");
String[] cookies = cookieValue.split(";");
for (String cookie: cookies) {
String[] split = cookie.split("=");
//if(split[0].equals("UserToken")){
webDriver.manage().addCookie(new Cookie(split[0].trim(),split[1].trim(),".csdn.net","/",null));
//}
}
TimeUnit.SECONDS.sleep(1);
Set<Cookie> cookies1 = webDriver.manage().getCookies();
for (Cookie cookie: cookies1) {
System.out.println(cookie);
}
//webDriver.navigate().refresh();
webDriver.get("https://mp.csdn.net");
TimeUnit.SECONDS.sleep(3);
String pageSource = webDriver.getPageSource();
//System.out.println(pageSource);
System.out.println("over....");
//webDriver.close();
//webDriver.quit();
}