提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
使用步骤
public class LzqUtils {
public static void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver_win32/chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
if(!cookieLogin(webDriver)) {
pLogin(webDriver);
}
return;
}
private static boolean cookieLogin(WebDriver webDriver) {
BufferedReader bufferedReader;
webDriver.get("网站");
try {
File cookieFile = new File("D:\\vue\\token/cookie.txt");
FileReader fileReader = new FileReader(cookieFile);
bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null){
StringTokenizer stringTokenizer = new StringTokenizer(line, ";");
while (stringTokenizer.hasMoreTokens()){
String name = stringTokenizer.nextToken();
String value = stringTokenizer.nextToken();
Cookie cookie = new Cookie(name, value);
webDriver.manage().addCookie(cookie);
}
}
webDriver.navigate().refresh();
WebElement element = webDriver.findElement(By.xpath("//button[@class='btn-md btn-theme']"));
if(element!=null){
return false;
}
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
private static void pLogin(WebDriver webDriver) throws InterruptedException {
File cookieFile = new File("D:/vue/token/cookie.txt");
try {
if(cookieFile.exists()) {
cookieFile.delete();
}
cookieFile.createNewFile();
FileWriter fileWriter = new FileWriter(cookieFile);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (Cookie cookie:webDriver.manage().getCookies()){
bufferedWriter.write((cookie.getName()+";"+
cookie.getValue()));
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}