在这篇文章中,我们将带领您了解在由Selenium驱动的PhantomJSC#中设置屏幕大小的全貌,包括selenium窗口最大化的相关情况。同时,我们还将为您介绍有关16、web爬虫讲解2—Phan
在这篇文章中,我们将带领您了解在由Selenium驱动的PhantomJS C#中设置屏幕大小的全貌,包括selenium窗口最大化的相关情况。同时,我们还将为您介绍有关16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS、C#使用Selenium+PhantomJS抓取数据、C#使用Selenium+PhantomJS抓取数据详解、centos下的selenium+phantomjs的知识,以帮助您更好地理解这个主题。
本文目录一览:- 在由Selenium驱动的PhantomJS C#中设置屏幕大小(selenium窗口最大化)
- 16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS
- C#使用Selenium+PhantomJS抓取数据
- C#使用Selenium+PhantomJS抓取数据详解
- centos下的selenium+phantomjs
在由Selenium驱动的PhantomJS C#中设置屏幕大小(selenium窗口最大化)
如何设置浏览器的屏幕尺寸?在设置PhantomJS之后,当我拍摄如下屏幕快照时,它仅显示400px宽的图像。
var driverService = PhantomJSDriverService.CreateDefaultService();driverService.HideCommandPromptWindow = true;driverService.LoadImages = false;driverService.ProxyType = "none";using (var driver = new PhantomJSDriver(driverService)){ etc....}
答案1
小编典典它应该与在其他任何浏览器中设置窗口大小相同,请看以下示例:如何使用Selenium
WebDriver获取窗口大小,调整大小或最大化窗口。
这是经过测试的代码对我有用:
var driverService = PhantomJSDriverService.CreateDefaultService();driverService.HideCommandPromptWindow = true;driverService.LoadImages = false;driverService.ProxyType = "none";using (var driver = new PhantomJSDriver(driverService)) { driver.Manage().Window.Size = new Size(1920, 1080); // Size is a type in assembly "System.Drawing" driver.Url = "http://www.stackoverflow.com"; driver.TakeScreenshot().SaveAsFile(@"c:\phantomjs_screenshot.png", ImageFormat.Png);}
16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS
【百度云搜索,搜各种资料:http://www.bdyss.cn】
【搜网盘,搜各种资料:http://www.swpan.cn】
PhantomJS虚拟浏览器
phantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,利用这个软件,可以获取到网址js加载的任何信息,也就是可以获取浏览器异步加载的信息
下载网址:http://phantomjs.org/download... 下载对应系统版本
下载后解压PhantomJS文件,将解压文件夹,剪切到python安装文件夹
然后将PhantomJS文件夹里的bin文件夹添加系统环境变量
cdm 输入命令:PhantomJS 出现以下信息说明安装成功
selenium模块是一个python操作PhantomJS软件的一个模块
selenium模块PhantomJS软件
webdriver.PhantomJS()实例化PhantomJS浏览器对象
get(''url'')访问网站
find_element_by_xpath(''xpath表达式'')通过xpath表达式找对应元素
clear()清空输入框里的内容
send_keys(''内容'')将内容写入输入框
click()点击事件
get_screenshot_as_file(''截图保存路径名称'')将网页截图,保存到此目录
page_source获取网页htnl源码
quit()关闭PhantomJS浏览器
#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver #导入selenium模块来操作PhantomJS
import os
import time
import re
llqdx = webdriver.PhantomJS() #实例化PhantomJS浏览器对象
llqdx.get("https://www.baidu.com/") #访问网址
# time.sleep(3) #等待3秒
# llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图保存到此目录
#模拟用户操作
llqdx.find_element_by_xpath(''//*[@id="kw"]'').clear() #通过xpath表达式找到输入框,clear()清空输入框里的内容
llqdx.find_element_by_xpath(''//*[@id="kw"]'').send_keys(''叫卖录音网'') #通过xpath表达式找到输入框,send_keys()将内容写入输入框
llqdx.find_element_by_xpath(''//*[@id="su"]'').click() #通过xpath表达式找到搜索按钮,click()点击事件
time.sleep(3) #等待3秒
llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图,保存到此目录
neir = llqdx.page_source #获取网页内容
print(neir)
llqdx.quit() #关闭浏览器
pat = "<title>(.*?)</title>"
title = re.compile(pat).findall(neir) #正则匹配网页标题
print(title)
PhantomJS浏览器伪装,和滚动滚动条加载数据
有些网站是动态加载数据的,需要滚动条滚动加载数据
实现代码
DesiredCapabilities 伪装浏览器对象
execute_script()执行js代码
current_url获取当前的url
#!/usr/bin/env python
# -*- coding:utf8 -*-
from selenium import webdriver #导入selenium模块来操作PhantomJS
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities #导入浏览器伪装模块
import os
import time
import re
dcap = dict(DesiredCapabilities.PHANTOMJS)
dcap[''phantomjs.page.settings.userAgent''] = (''Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'')
print(dcap)
llqdx = webdriver.PhantomJS(desired_capabilities=dcap) #实例化PhantomJS浏览器对象
llqdx.get("https://www.jd.com/") #访问网址
#模拟用户操作
for j in range(20):
js3 = ''window.scrollTo(''+str(j*1280)+'',''+str((j+1)*1280)+'')''
llqdx.execute_script(js3) #执行js语言滚动滚动条
time.sleep(1)
llqdx.get_screenshot_as_file(''H:/py/17/img/123.jpg'') #将网页截图,保存到此目录
url = llqdx.current_url
print(url)
neir = llqdx.page_source #获取网页内容
print(neir)
llqdx.quit() #关闭浏览器
pat = "<title>(.*?)</title>"
title = re.compile(pat).findall(neir) #正则匹配网页标题
print(title)
【转载自:http://www.lqkweb.com】
C#使用Selenium+PhantomJS抓取数据
C#使用Selenium+PhantomJS抓取数据
参考:https://www.cnblogs.com/endlock/p/6423613.html
非原创,来自上面的链接
手头项目需要抓取一个用js渲染出来的网站中的数据。使用常用的httpclient抓回来的页面是没有数据。上网百度了一下,大家推荐的方案是使用PhantomJS。PhantomJS是一个没有界面的webkit浏览器,能够和浏览器效果一致的使用js渲染页面。Selenium是一个web测试框架。使用Selenium来操作PhantomJS绝配。但是网上的例子多是Python的。无奈,下载了python按照教程搞了一下,卡在了Selenium的导入问题上。遂放弃,还是用自己惯用的c#吧,就不信c#上没有。经过半个小时的折腾,搞定(python折腾了一个小时)。记录下这篇博文,让我等搞c#的新手能用上PhantomJS。
第一步:打开visual studio 2017 新建一个控制台项目,打开nuget包管理器。
第二部:搜索Selenium,安装Selenium.WebDriver。注意:如果要使用代理的话最好安装3.0.0版本。
第三步:写下如下图所示的代码。但是执行的时候会报错。原因是找不到PhantomJS.exe。这时候可以去下载一个,也可以继续看第四步。

using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
var url = "http://www.baidu.com";
IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService());
driver.Navigate().GoToUrl(url);
Console.WriteLine(driver.PageSource);
Console.Read();
}
private static PhantomJSDriverService GetPhantomJSDriverService()
{
PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService();
//设置代理服务器地址
//pds.Proxy = $"{ip}:{port}";
//设置代理服务器认证信息
//pds.ProxyAuthentication = GetProxyAuthorization();
return pds;
}
}
}

第四步:打开nuget安装Selenium.PhantomJS.WebDriver包。
第五步:运行。可以看到phantomjs.exe被自动下载了。
好了,这样就可以开始你的数据抓取大业了。
C#使用Selenium+PhantomJS抓取数据详解
手头项目需要抓取一个用js渲染出来的网站中的数据。使用常用的httpclient抓回来的页面是没有数据。上网百度了一下,大家推荐的方案是使用phantomjs。phantomjs是一个没有界面的webkit浏览器,能够和浏览器效果一致的使用js渲染页面。selenium是一个web测试框架。使用selenium来操作phantomjs绝配。但是网上的例子多是python的。无奈,下载了python按照教程搞了一下,卡在了selenium的导入问题上。遂放弃,还是用自己惯用的c#吧,就不信c#上没有。经过半个小时的折腾,搞定(python折腾了一个小时)。记录下这篇博文,让我等搞c#的新手能用上phantomjs。
第一步:打开visual studio 2017 新建一个控制台项目,打开nuget包管理器。
第二部:搜索Selenium,安装Selenium.WebDriver。注意:如果要使用代理的话最好安装3.0.0版本。
第三步:写下如下图所示的代码。但是执行的时候会报错。原因是找不到PhantomJS.exe。这时候可以去下载一个,也可以继续看第四步。
using OpenQA.Selenium;using OpenQA.Selenium.PhantomJS;using System;namespace ConsoleApp1 { class Program { static void Main(string[] args) { var url = "http://www.baidu.com"; IWebDriver driver = new PhantomJSDriver(GetPhantomJSDriverService()); driver.Navigate().GoToUrl(url); Console.WriteLine(driver.PageSource); Console.Read(); } private static PhantomJSDriverService GetPhantomJSDriverService() { PhantomJSDriverService pds = PhantomJSDriverService.CreateDefaultService(); //设置代理服务器地址 //pds.Proxy = $"{ip}:{port}"; //设置代理服务器认证信息 //pds.ProxyAuthentication = GetProxyAuthorization(); return pds; } } }
第四步:打开nuget安装Selenium.PhantomJS.WebDriver包。
第五步:运行。可以看到phantomjs.exe被自动下载了。
好了,这样就可以开始你的数据抓取大业了。
以上就是C#使用Selenium+PhantomJS抓取数据详解的详细内容,更多请关注php中文网其它相关文章!
centos下的selenium+phantomjs
From:http://www.easonhan.info/phantomjs/2015/01/10/ghost-driver/
参考:http://blog.chinaunix.net/uid-10289334-id-4473078.html
做过selenium自动化项目的同学应该都遇到过这样的问题:测试用例太多,运行速度过慢导致团队成员怨声载道。
于是便有了selenium grid和多线程运行selenium测试用例的方法。这些方法各有利弊这里就不一一列举了。但总的来说,如果浏览器运行的速度足够快,那么多线程并发时的用例执行速度应该是可以满足实际项目需求的。
再想象一下这样的情景:如果你手头的机器是没有gui的(这是可能的,我以前的几台centos的server根本就没有ui),如何在这样的headless的机器上运行selenium用例呢?
答案是可以用selenium自带的HtmlUnitDriver。不过可惜的是HtmlUnitDriver对js的支持不是特别完美,所以该方案可行但是不完善,不是特别适合用于真实项目。
好在现在有了phantomjs和ghostdriver,我们可以用ghostdriver来运行selenium测试用例。所有的用例都是在没有gui的浏览器里运行,运行速度可以得到极大的提升。再加上phantomjs是基于webkit的,所以ghostdriver完全可以模拟chrome和safari的行为。
在我的macbookpro上,chromedriver的表现不是特别令人满意,而我又没有安装firefox和safari driver,所以对于一般的页面(js交互不是特别多的页面),我都是用ghost driver在调试问题,快速而简便。最主要是没有真实的浏览器弹出来,不会像chromedriver那样经常意外退出造成内存泄漏,也不会像firefox那样运行缓慢。
下面简单介绍一下ghost driver 与selenium合体的过程。
背景知识
下面的内容要求你已经成功的安装好python的selenim binding。如果你有pip,直接运行pip install selenium
即可。如果被墙,请使用豆瓣源。
或者成功的安装好ruby的watir-webdriver。如果你有gem,直接运行gem install watir-webdriver
即可。如果被墙,请使用淘宝源。
安装ghost driver
ghost driver现在已经跟phantomjs合体,所以安装好最新版本的phantomjs就等于安装好了ghostdriver。
在这里下载对应平台的phantomjs。
-
首先解压下载好的zip文件或tar文件(linux only);
-
windows用户将解压过后的得到的phantomjs.exe文件加入系统的PATH中。简单点说如果你使用pyhon,就把phantomjs.exe放到python的安装目录下,ruby用户放到ruby/bin目录下;
-
mac和linux用户可以把解压后得到的phantomjs建个软链到/usr/local/bin目录下。
ln -s /where/is/phantomjs /usr/local/bin/phantomjs
; -
快速开始
python用户新建itest.py文件然后敲入下面的内容
from selenium import webdriver dr = webdriver.PhantomJS('phantomjs') drget'http://baidu.info') print title current_url quit()
watir-webdriver用户新建文件itest.rb然后敲入下面的内容
讨论
-
ghostdriver尽管对js的支持是不错的,但是如果你的页面上js交互过多的话,ghostdriver是会缴械投降的;
-
用ghostdriver+selenium的语法可以做一些不错的爬虫;
-
用java用户请使用maven下载java的ghostdriver binding;
-
当页面上有flash播放器时,phantom可能会萌萌哒的卡在那里一动不动;
-
ghostdriver基于phantomjs,phantomjs可以做爬虫,简单的性能测试,ui自动化测试和其他一些工作;
-
由于没有ui,当测试发生错误的时候调试的工作量就会变大;
看不到运行的过程,心中惶恐不安怎么办
答案是截图拯救测试人员,截图拯救世界。
运行到关键的节点或步骤时截个图,即方便了调试又使你的测试拥有足够多的输出,一举两得何乐不为?
python代码
总结
以上是小编为你收集整理的centos下的selenium+phantomjs全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
关于在由Selenium驱动的PhantomJS C#中设置屏幕大小和selenium窗口最大化的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于16、web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS、C#使用Selenium+PhantomJS抓取数据、C#使用Selenium+PhantomJS抓取数据详解、centos下的selenium+phantomjs等相关知识的信息别忘了在本站进行查找喔。
本文标签: