使用selenium进行爬网:如何在java中关闭驱动程序后保持登录状态

naf*_*fas 4 java selenium selenium-webdriver

无论如何,那司机能记住logged in session,所以它不会带我回到Login页面(例如喜欢google-chrome)吗?

这就是我现在正在做的事情

public static void main(String[] args) throws Exception {

    driver = new ChromeDriver();
    driver.get("http://localhost/interestingStuff);  //I get redirected to login page 

    login(); //logins in to the page and submits(note I try to omit this part if possible).
    doStuff();

    driver.close(); //I want to be able to keep session next time I start this program.


}
Run Code Online (Sandbox Code Playgroud)

And*_*rew 7

具有默认配置的Selenium为每个会话创建临时配置文件,并在工作后删除此临时文件.

因此,如果您想保存cookie(remmember"登录会话"),您需要通过以下方式之一:

  1. 使用之前创建的相同Chrome驱动程序配置文件(使用Selenium WebDriver加载Chrome配置文件)
  2. 创建用于将cookie保存到某个文件的外部类(此处为c#解决方案:保持用户登录 - 使用Web驱动程序保存cookie)