使用PhantomJS的webdriver ExecutePhantomJS过滤掉图像的C#示例 [英] C# example of using PhantomJS webdriver ExecutePhantomJS to filter out images

查看:2467
本文介绍了使用PhantomJS的webdriver ExecutePhantomJS过滤掉图像的C#示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待能够有选择地打开和关闭特定的图像。我遇到下面的文章:

I am looking to be able to selectively turn on and off certain images. I've come across the following article:

PhantomJS如何跳过下载资源

我还发现这篇文章是使用python很相似:

I've also found this article which is very similar using python:

硒通过PhantomJS / Python的驱动

我假设我可以通过 webdriver.ExecutePhantomJS(字符串脚本,params对象[]参数)。

我不知道是,如果我需要创建一些页面第一个对象通过硒PageFactory,然后调用这个函数呢?我怎么会再次将其关闭。如何做到这一点的例子是非常有益的。

What I don't know is if I need to create some page object first via the Selenium PageFactory and then call this function? How would I turn it off again. An example of how to do this would be very helpful.

推荐答案

我只是在寻找类似的东西...

I was just looking for something similar...

本,例如,会忽略所有网址以巴纽结尾的:

This, for example, will ignore all urls ending with ".png":

using (var driver = new PhantomJSDriver())
{
    const string phantomScript = "var page=this;page.onResourceRequested = function(requestData, request) { var reg =  /\\.png/gi; var isPng = reg.test(requestData['url']); console.log(isPng,requestData['url']); if (isPng){console.log('Aborting: ' + requestData['url']);request.abort();}}";
    var script = driver.ExecutePhantomJS(phantomScript);
    driver.Navigate().GoToUrl("https://www.google.com/");                
    driver.GetScreenshot().SaveAsFile("googlewithoutimage.png",ImageFormat.Png);
}   



注意,如果您正在寻找的页对象这个在ExecutePhantomJS范围内,也注意到我写的日志,以获得更好的了解所发生的事情的。

note that the 'page' object that you're looking for is 'this' in the ExecutePhantomJS scope, also note that I'm writing to the log to get a better understanding of what's happening.

这使您可以灵活地开启或关闭图像选择,当你需要的。

This gives you the flexibility to turn on or off images selectively, as you required.

这篇关于使用PhantomJS的webdriver ExecutePhantomJS过滤掉图像的C#示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆