我们正在选择我们的无头浏览器驱动程序解决方案,这将是Selenium WebDriver的一些实现.还有就是GhostDriver,利用的是PhantomJS在在一侧和后端HtmlUnitDriver其基于的HtmlUnit另一方.
PhantomJS使用Safari的渲染引擎WebKit来渲染页面,而HtmlUnitDriver使用其他浏览器没有使用的Rhino引擎(它只是"模拟"浏览器行为.最后一个事实被视为con,因为渲染行为可能与流行的浏览器.
我们认为,PhantomJS是一个更强大的候选人.但是,我们并不知道所有事情:)我们的决定是否还应考虑其他因素和权衡取舍?HtmlUnitDriver可以成为更好的解决方案的其他场景?
htmlunit phantomjs selenium-webdriver ghostdriver htmlunit-driver
问题是直截了当的,但有些背景可能有所帮助.
我正在尝试使用selenium和phantomjs作为下载程序来部署scrapy.但问题是它在尝试部署时继续说权限被拒绝.所以我想改变ghostdriver.log的路径或者只是禁用它.看着phantomjs -h和ghostdriver github页面我找不到答案,我的朋友google也让我失望了.
$ scrapy deploy
Building egg of crawler-1370960743
'build/scripts-2.7' does not exist -- can't clean it
zip_safe flag not set; analyzing archive contents...
tests.fake_responses.__init__: module references __file__
Deploying crawler-1370960743 to http://localhost:6800/addversion.json
Server response (200):
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/scrapyd/webservice.py", line 18, in render
return JsonResource.render(self, txrequest)
File "/usr/lib/pymodules/python2.7/scrapy/utils/txweb.py", line 10, in render
r = resource.Resource.render(self, txrequest)
File "/usr/lib/python2.7/dist-packages/twisted/web/resource.py", line 216, in render
return m(request)
File "/usr/lib/pymodules/python2.7/scrapyd/webservice.py", line 66, in render_POST
spiders = get_spider_list(project)
File "/usr/lib/pymodules/python2.7/scrapyd/utils.py", …Run Code Online (Sandbox Code Playgroud) 我试图弄清楚如何通过HTTP代理路由我的请求.
我正在初始化这样的webdriver:
user_agent = 'my user agent 1.0'
DesiredCapabilities.PHANTOMJS['phantomjs.page.settings.userAgent'] = user_agent
driver = webdriver.PhantomJS()
Run Code Online (Sandbox Code Playgroud)
我已经浏览了文档和源代码,似乎无法通过webdriver找到使用phantomjs代理服务器的方法.
有什么建议?
我试图运行PhantomJS从内selenium.webdriverCentOS的服务器上.PhantomJS在路径中并且从终端正常运行.但是在脚本中它似乎已启动,但之后无法在指定端口上到达(我尝试了2个不同的打开端口,来自我的提供程序29842和60099,它们都不工作,并且没有指定端口也没有启动它).
错误发生在selenium.webdriver.common.utils:
try:
socket_ = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_.settimeout(1)
socket_.connect(("localhost", port))
socket_.close()
return True
except socket.error:
return False
Run Code Online (Sandbox Code Playgroud)
这是从我的脚本(我尝试没有任何参数,以及编写可执行文件的完整路径,但都没有工作):
self.browser = webdriver.PhantomJS(
port=29842,
desired_capabilities={
'javascriptEnabled': True,
'platform': 'windows',
'browserName': 'Mozilla',
'version': '5.0',
'phantomjs.page.settings.userAgent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
}
)
Run Code Online (Sandbox Code Playgroud)
这是初始化webdriver的脚本selenium.webdriver.phantomjs.service.我检查并subprocess.Popen实际上lauches phantomjs,错误发生在while循环:
try:
self.process = subprocess.Popen(self.service_args,
stdout=self._log, stderr=self._log)
except Exception as e:
raise WebDriverException("Unable to start phantomjs with ghostdriver.", e)
count = 0 …Run Code Online (Sandbox Code Playgroud) 我正在使用WebDriver和Firefox运行几个测试.
我遇到以下命令的问题:
WebDriver.get(www.google.com);
使用此命令,WebDriver将阻塞,直到触发onload事件.虽然这通常需要几秒钟,但在从未完成加载的网站上可能需要数小时.
我想做的是在一定的超时后停止加载页面,以某种方式模拟Firefox的停止按钮.
我每次尝试加载页面时都首先尝试执行以下JS代码:
var loadTimeout=setTimeout(\"window.stop();\", 10000);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不起作用,可能是因为:
由于脚本的加载顺序,stop()方法无法停止加载脚本1所包含的文档
更新1:我尝试使用SquidProxy来添加连接和请求超时,但问题仍然存在.
我今天发现的一个奇怪的事情是,我的机器上永远不会停止加载的一个网站(FF3.6 - 4.0和Mac Os 10.6.7)正常加载到其他浏览器和/或计算机上.
更新2:显然可以通过告诉Firefox不加载图像来解决问题.希望在此之后一切都会奏效......
我希望WebDriver有一个更好的Chrome驱动程序才能使用它.Firefox每天让我失望!
更新3:Selenium 2.9添加了一项新功能来处理驱动程序似乎挂起的情况.这可以使用FirefoxProfile如下:
FirefoxProfile firefoxProfile = new ProfilesIni().getProfile("web");
firefoxProfile.setPreference("webdriver.load.strategy", "fast");
Run Code Online (Sandbox Code Playgroud)
我会在尝试之后发布这是否有效.
更新4:最后,上述方法均无效.我最终"杀死"了很长时间才完成的线程.我正在尝试使用Ghostdriver,这是一个使用PhantomJS作为后端的远程WebDriver.PhantomJS是一个无头的WebKit脚本,所以我希望不会出现像Firefox这样的真正浏览器的问题.对于没有义务使用firefox(爬行目的)的人,我将更新结果
更新5:更新的时间.使用5个月的ghostdriver 1.1而不是FirefoxDriver,我可以说我对他的性能和稳定性非常满意.我遇到了一些我们没有适当行为但看起来像一般ghostdriver足够稳定的情况.因此,如果您需要像我这样的浏览器来进行爬行/网页抓取,我建议您使用ghostdriver而不是firefox和xvfb,这会给您带来几个令人头疼的问题......
根据这个,现在可以修改标题.我需要在PhantomJS webdriver中修改Accept-Language.此代码不起作用
DesiredCapabilities.PHANTOMJS['phantomjs.page.customHeaders.Accept-Language'] = 'ru-RU'
driver = webdriver.PhantomJS()
Run Code Online (Sandbox Code Playgroud)
有可能以某种方式配置Phantomjs发送我的标题?我不关心在哪里:内部ghostdriver,phantomjs或phantomjs-webdriver.
我正在使用GhostDriver的Java绑定来对PhantomJS运行Selenium验收测试.
如果PhantomJS请求的其中一个网页通过console.log登录到Javascript控制台,是否有办法捕获或查看这些消息?
我猜这个问题的答案很简单,但是我无法解决这个问题!
我正在使用Selenium开发一个小型控制台应用程序,我需要关闭它的所有日志.
我试过phantomJSDriver.setLogLevel(Level.OFF);但它不起作用.我需要帮助.
如何禁用使用Selenium和Phantomjs(GhostDriver)的控制台应用程序中的所有日志?
在PhantomJS 1.9.2上,ubuntu 12 LTS和Ghostdirver 1.04以及selenium 2.35我在测试后得到了悬空的phantomjs进程.任何人都知道如何解决这个问题的好方法?
这是一个演示奇怪行为的测试程序:
package testing;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
public class PhantomIsNotKilledDemo {
private static WebDriver getDriver(){
String browserPathStr = System.getProperty("selenium.pathToBrowser");
if (browserPathStr == null) browserPathStr = "/home/user1/apps/phantomjs/bin/phantomjs";
DesiredCapabilities caps = DesiredCapabilities.phantomjs();
caps.setCapability("takesScreenshot", true);
caps.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
browserPathStr );
WebDriver driver = new PhantomJSDriver(caps);
return driver;
}
public static void main(String[] args) {
int max = 10;
for (int i = 0; i < max; i++){
WebDriver d1 = getDriver();
d1.get("http://www.imdb.com/title/tt1951264");
System.out.println("done with …Run Code Online (Sandbox Code Playgroud) 以下是创建Firefox配置文件的方法:
fp = webdriver.FirefoxProfile()
fp.set_preference("browser.download.folderList",2)
fp.set_preference("browser.download.dir", download_dir)
fp.set_preference("browser.download.manager.showWhenStarting",False)
fp.set_preference("browser.helperApps.neverAsk.saveToDisk","text/csv")
Run Code Online (Sandbox Code Playgroud)
你是如何使用PhantomJS(GhostDriver)的?
ghostdriver ×10
phantomjs ×9
python ×3
selenium ×3
webdriver ×2
firefox ×1
htmlunit ×1
java ×1
memory-leaks ×1
port ×1
proxy ×1
scrapy ×1
web-crawler ×1