如何通过Selenium WebDriver在PhantomJS中禁用JavaScript [英] How to disable JavaScript in PhantomJS through Selenium WebDriver

查看:96
本文介绍了如何通过Selenium WebDriver在PhantomJS中禁用JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用scrapy和selenium进行刮擦时禁用JavaScript。
这样做的动机是提高刮削速度。我发现了对Firefox驱动程序的偏好,但没有找到PhantomJS。

I want to disable JavaScript while scraping using scrapy and selenium. Moto of doing that is to increase scraping speed. I found the preference for Firefox driver but not PhantomJS.

firefox_profile = webdriver.FirefoxProfile()
firefox_profile.set_preference("javascript.enabled", False)

driver = webdriver.Firefox(firefox_profile=firefox_profile)
driver.get('http://www.quora.com/')

如何为PhantomJS webdriver做到这一点?

How can this be done for PhantomJS webdriver?

推荐答案

PhantomJS中的WebDriver协议是一个纯JavaScript实现,称为Ghostdriver。它大量使用 page.evaluate() 访问DOM,实际上没有其他方法可以访问DOM,与页面交互或使用PhantomJS做任何有意义的事情。你不应该这样做。

The WebDriver protocol in PhantomJS is a pure JavaScript implementation that is known as Ghostdriver. It makes heavy use of page.evaluate() to access the DOM and there is really no other way to access the DOM, interact with the page or do anything meaningful with PhantomJS. You shouldn't do this.

如果你仍然想要使用它,这应该有效:

If you still want to go through with it, this should work:

cap = webdriver.DesiredCapabilities.PHANTOMJS
cap["phantomjs.page.settings.javascriptEnabled"] = False
driver = webdriver.PhantomJS(desired_capabilities=cap)

这篇关于如何通过Selenium WebDriver在PhantomJS中禁用JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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