java - 在每次运行时为 PhantomJs 使用相同的 session

标签 java c# selenium selenium-webdriver phantomjs

我正在爬取一个安全网站,每当我重新启动爬虫应用程序时它都会阻止我(我需要更改 IP 作为技巧)。我通过在 chrome 驱动程序中使用默认用户配置文件解决了这个问题(我现在正在使用 C#,但如果需要我可以切换到 java):

ChromeOptions options = new ChromeOptions();
options.AddArguments($"user-data-dir=C:/Users/{Environment.UserName}/AppData/Local/Google/Chrome/User Data/Default");

它会保存所有 session 和 cookie,并在重新启动我的应用程序时恢复它们。一切都按预期进行。

现在,出于某些原因,我需要将我的网络驱动程序更改为 PhantomJS。

我的问题 我怎样才能使用 PhantomJS 使这种情况成为可能:登录到一个帐户(如 gmail 或 facebook),关闭我的应用程序和驱动程序,发现自己已登录-下次我运行应用程序和驱动程序。换句话说,如何在每次运行时为 PhantomJS 使用相同的 session ?

尝试 1(在 C# 中):

经过一些搜索,我发现这可以在 PhantomJS 中使用本地存储和 cookies 文件参数来完成。现在的问题是本地存储路径总是空的,那里什么也没有保存(我导航到多个站点但仍然是空的),因此,我不能使用以前执行的 session 。我设置本地存储和 cookie 文件的代码很简单,如下所示:

PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.LocalStoragePath = Application.StartupPath + "\\default";
service.CookiesFile = Application.StartupPath + "\\default\\Cookies";
IWebDriver driver = new PhantomJSDriver(service);

我的方法有什么问题?

尝试 2(在 C# 中):

根据@SiKing 的回答和评论讨论,我更改为以下代码(使用 AddArgument)但目录仍然是空的:

string localStoragePath = Path.Combine(Path.GetTempPath(),"PhantomLocalStorage-");

if (!Directory.Exists(localStoragePath))
{
    Directory.CreateDirectory(localStoragePath);
}

PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService();
service.AddArgument("--local-storage-quota=5000");
service.AddArgument("--local-storage-path=" + localStoragePath);
IWebDriver driver = new PhantomJSDriver(service);

尝试 3(在 java 中):

目录还是空的:

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
List<String> cliArgs = new ArrayList<String>();
Path local_storage_path = Paths.get(System.getProperty("java.io.tmpdir") + "PhantomLocalStorage-");
if (Files.notExists(local_storage_path)) {
    try {
        Files.createDirectory(local_storage_path);
    }
    catch (IOException e) {
        JOptionPane.showConfirmDialog(null, "Can Not Create Path");
    }
}
cliArgs.add("--local-storage-quota=5000");
cliArgs.add("--local-storage-path=" + local_storage_path.toString());
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs);
WebDriver driver = new PhantomJSDriver(capabilities);

最佳答案

PhantomJS 默认启动时没有本地存储;见this discussion .

为了通过 Selenium 启用本地存储,我使用了以下 Java 代码。抱歉,我使用 C# 已经太久了,但我相信 C# 绑定(bind)有类似的可用方法。

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
// Phantom options can only be set from CLI
List<String> cliArgs = new ArrayList<String>();
cliArgs.add("--local-storage-quota=5000");
Path local_storage_path = Files.createTempDirectory("PhantomLocalStorage-");
cliArgs.add("--local-storage-path=" + local_storage_path.toString());
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs);
WebDriver driver = new PhantomJSDriver(capabilities);

请注意,local_storage_path 在您完成后不会被删除。如果需要,可以根据 this post 设置一个 Hook 。 .但我怀疑在 C# 中,这部分将与 Java 大不相同。

关于java - 在每次运行时为 PhantomJs 使用相同的 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44329087/

相关文章:

c# - 在 VS2010 Express 中创建构造函数的代码片段

Selenium Webdriver - 当 baseurl 包含应用程序的 IP 地址时,脚本不运行

java - Selenium在多个浏览器上并行测试(JAVA)

java - 有没有办法检查字符串数组上是否有下一个元素?

java - 来自 API 调用的字符串翻译

java - 如何作为浏览器下载文件

java - 如果站点正在使用 Ajax,如何使用 Selenium WebDriver 检查?

java - 我如何允许 JNLP 启动的软件访问套接字?

c# - 我怎样才能删除目录,包括里面的文件?

c# - 如何在单个 Razor View 中编辑多个模型