如何在 python selenium 中为 PhantomJS 禁用屏幕截图和 javascript?
How to disable screenshots and javascript for PhantomJS in python selenium?
我正在 windows 上使用 phantomJS 在 python/selenium 框架中进行抓取。首先,我尝试使用 selenium:
禁用 javascript 和 screenhsots
driver = webdriver.PhantomJS("phantomjs.exe", desired_capabilities = dcap)
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.javascriptEnabled"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.takesScreenshot"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.clearMemoryCash"] = False
但是,当我查看 ghostdriver.log
时,Session.negotiatedCapabilities
包括:
browserName:phantomjs
version:2.1.1
driverName:ghostdriver
driverVersion:1.2.0
platform:windows-7-32bit
javascriptEnabled:true # Should be false
takesScreenshot:true # Should be false
因此,我认为我需要在 onInitialized=function()
期间禁用这两个参数,类似于下面的代码片段:
phantom_exc_uri='/session/$sessionId/phantom/execute'
driver.command_executor._commands['executePhantomScript'] = ('POST', phantom_exc_uri)
initScript="""
this.onInitialized=function() {
var page=this;
### disable javascript and screenshots here ###
}
"""
driver.execute('executePhantomScript',{'script': initScript, 'args': []})
Q1:为什么我可以在webdriver.DesiredCapabilities
中设置一些 phantomJS规范,而其他的却不行?这是我的错误还是一些错误?
Q2:在onInitialized期间完成这个是否合理,还是我走错了路?
Q2:如果可以,如何在onInitialized期间禁用JS和截图?
你在问题中提出了不少问题。让我尝试解决所有这些问题。 Selenium v3.8.1
、ghostdriver v1.2.0
和 phantomjs v2.1.1 Browser
的简单工作流向我们展示了以下 Session.negotiatedCapabilities 默认情况下传递:
"browserName":"phantomjs"
"version":"2.1.1"
"driverName":"ghostdriver"
"driverVersion":"1.2.0"
"platform":"windows-8-32bit"
"javascriptEnabled":true
"takesScreenshot":true
"handlesAlerts":false
"databaseEnabled":false
"locationContextEnabled":false
"applicationCacheEnabled":false
"cssSelectorsEnabled":true
"webStorageEnabled":false
"rotatable":false
"acceptSslCerts":false
"nativeEvents":true
"proxy":{"proxyType":"direct"}}
所以默认情况下,要通过 PhantomJSDriver
和 Ghost Browser
组合建立成功的会话,至少需要以下 Capabilities
要求。
然后用户可以使用 DesiredCapabilities
class 来调整功能。但是,某些功能是创建成功的 Ghost Browser
会话的最低要求。
javascriptEnabled 就是这样一个 属性 是必须的。直到前几个版本 Selenium
才允许将 javascriptEnabled 属性调整为 false。但是现在 WebDriver
是 W3C Recommendation Candidate
强制性功能不能再通过 DesiredCapabilities
覆盖用户等级。
即使您尝试在 user level
处调整它们,WebDriver
也会在配置 capabilities
.
时将它们覆盖为默认值
所以,尽管您尝试了以下方法:
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.javascriptEnabled"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.takesScreenshot"] = False
属性 javascriptEnabled 和 takesScreenshot 默认为必需的强制配置。
更新
正如您在评论 What about changing those AFTER the Ghostdriver session is established, i.e. page.onInitialized
中提到的,直接的答案是 否。
一旦capabilities
被冻结并且协商初始化一个Browsing Session
capabilities
在特定 session is active
之前一直成立。因此,一旦 session is established
,您就无法更改任何 capabilities
。要更改 capabilities
,您必须再次配置 WebDriver
实例。
我正在 windows 上使用 phantomJS 在 python/selenium 框架中进行抓取。首先,我尝试使用 selenium:
禁用 javascript 和 screenhsotsdriver = webdriver.PhantomJS("phantomjs.exe", desired_capabilities = dcap)
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.javascriptEnabled"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.takesScreenshot"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.clearMemoryCash"] = False
但是,当我查看 ghostdriver.log
时,Session.negotiatedCapabilities
包括:
browserName:phantomjs
version:2.1.1
driverName:ghostdriver
driverVersion:1.2.0
platform:windows-7-32bit
javascriptEnabled:true # Should be false
takesScreenshot:true # Should be false
因此,我认为我需要在 onInitialized=function()
期间禁用这两个参数,类似于下面的代码片段:
phantom_exc_uri='/session/$sessionId/phantom/execute'
driver.command_executor._commands['executePhantomScript'] = ('POST', phantom_exc_uri)
initScript="""
this.onInitialized=function() {
var page=this;
### disable javascript and screenshots here ###
}
"""
driver.execute('executePhantomScript',{'script': initScript, 'args': []})
Q1:为什么我可以在webdriver.DesiredCapabilities
中设置一些 phantomJS规范,而其他的却不行?这是我的错误还是一些错误?
Q2:在onInitialized期间完成这个是否合理,还是我走错了路?
Q2:如果可以,如何在onInitialized期间禁用JS和截图?
你在问题中提出了不少问题。让我尝试解决所有这些问题。 Selenium v3.8.1
、ghostdriver v1.2.0
和 phantomjs v2.1.1 Browser
的简单工作流向我们展示了以下 Session.negotiatedCapabilities 默认情况下传递:
"browserName":"phantomjs"
"version":"2.1.1"
"driverName":"ghostdriver"
"driverVersion":"1.2.0"
"platform":"windows-8-32bit"
"javascriptEnabled":true
"takesScreenshot":true
"handlesAlerts":false
"databaseEnabled":false
"locationContextEnabled":false
"applicationCacheEnabled":false
"cssSelectorsEnabled":true
"webStorageEnabled":false
"rotatable":false
"acceptSslCerts":false
"nativeEvents":true
"proxy":{"proxyType":"direct"}}
所以默认情况下,要通过 PhantomJSDriver
和 Ghost Browser
组合建立成功的会话,至少需要以下 Capabilities
要求。
然后用户可以使用 DesiredCapabilities
class 来调整功能。但是,某些功能是创建成功的 Ghost Browser
会话的最低要求。
javascriptEnabled 就是这样一个 属性 是必须的。直到前几个版本 Selenium
才允许将 javascriptEnabled 属性调整为 false。但是现在 WebDriver
是 W3C Recommendation Candidate
强制性功能不能再通过 DesiredCapabilities
覆盖用户等级。
即使您尝试在 user level
处调整它们,WebDriver
也会在配置 capabilities
.
所以,尽管您尝试了以下方法:
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.page.settings.javascriptEnabled"] = False
webdriver.DesiredCapabilities.PHANTOMJS["phantomjs.takesScreenshot"] = False
属性 javascriptEnabled 和 takesScreenshot 默认为必需的强制配置。
更新
正如您在评论 What about changing those AFTER the Ghostdriver session is established, i.e. page.onInitialized
中提到的,直接的答案是 否。
一旦capabilities
被冻结并且协商初始化一个Browsing Session
capabilities
在特定 session is active
之前一直成立。因此,一旦 session is established
,您就无法更改任何 capabilities
。要更改 capabilities
,您必须再次配置 WebDriver
实例。