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

时间:2015-10-01 11:22:44

标签: 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.


}

1 个答案:

答案 0 :(得分:6)

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

因此,如果您想保存cookie(重新加入“登录会话”),您需要通过以下方式之一:

  1. 使用之前创建的相同Chrome驱动程序配置文件(Load Chrome Profile using Selenium WebDriver
  2. 创建用于将Cookie保存到某个文件的外部类(此处为c#solution:Keep user logged in - save cookies using web driver