Python Selenium-如何在初始化PhantomJS驱动程序实例时配置客户端证书文件

鹰28

我需要使用证书(.cer)才能连接到网站。我将PhantomJS与Python结合使用。这是连接代码:

driver = webdriver.PhantomJS(executable_path="Selenium/bin/phantomjs", 
service_args=['--ssl-client-certificate-file=certificate.cer'],
desired_capabilities=dcap)

但是,返回的HTML是:

<html><head></head><body></body></html>

URL是:

about:blank

但是,如果我删除:

'--ssl-client-certificate-file=certificate.cer'

然后,我得到了正确的URL和一些HTML内容(由于缺少证书,所以禁止显示消息)。

知道我该怎么做才能解决这个问题?

更新1 Ghostdriver.log包含以下内容:

[INFO  - 2018-04-27T15:18:44.680Z] GhostDriver - Main - running on port 55768
[INFO  - 2018-04-27T15:18:44.960Z] Session [46addab0-4a2e-11e8-879b-bb8782c84ba6] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2018-04-27T15:18:44.960Z] Session [46addab0-4a2e-11e8-879b-bb8782c84ba6] - page.customHeaders:  - {}
[INFO  - 2018-04-27T15:18:44.960Z] Session [46addab0-4a2e-11e8-879b-bb8782c84ba6] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.1.1","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"mac-unknown-64bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"},"phantomjs.page.settings.javascriptEnabled":true}
[INFO  - 2018-04-27T15:18:44.960Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 46addab0-4a2e-11e8-879b-bb8782c84ba6
[INFO  - 2018-04-27T15:19:26.438Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW

更新2-使用logging.getLogger()

2018-04-30 09:39:56,730 - selenium.webdriver.remote.remote_connection - DEBUG - POST http://127.0.0.1:57672/wd/hub/session {"requiredCapabilities": {}, "desiredCapabilities": {"platform": "ANY", "browserName": "phantomjs", "version": "", "phantomjs.page.settings.javascriptEnabled": true, "javascriptEnabled": true}}
2018-04-30 09:39:56,745 - selenium.webdriver.remote.remote_connection - DEBUG - Finished Request
2018-04-30 09:39:56,745 - selenium.webdriver.remote.remote_connection - DEBUG - POST http://127.0.0.1:57672/wd/hub/session/0f971930-4c52-11e8-8957-cd8a79dc644c/url {"url": "https://idp.iamfas.belgium.be/fasui/login/commercialnoidservice", "sessionId": "0f971930-4c52-11e8-8957-cd8a79dc644c"}
2018-04-30 09:39:56,971 - selenium.webdriver.remote.remote_connection - DEBUG - Finished Request
2018-04-30 09:39:56,971 - selenium.webdriver.remote.remote_connection - DEBUG - GET http://127.0.0.1:57672/wd/hub/session/0f971930-4c52-11e8-8957-cd8a79dc644c/source {"sessionId": "0f971930-4c52-11e8-8957-cd8a79dc644c"}
2018-04-30 09:39:56,974 - selenium.webdriver.remote.remote_connection - DEBUG - Finished Request
HTML FOUND:
<html><head></head><body></body></html>
2018-04-30 09:39:56,974 - selenium.webdriver.remote.remote_connection - DEBUG - GET http://127.0.0.1:57672/wd/hub/session/0f971930-4c52-11e8-8957-cd8a79dc644c/url {"sessionId": "0f971930-4c52-11e8-8957-cd8a79dc644c"}
2018-04-30 09:39:56,980 - selenium.webdriver.remote.remote_connection - DEBUG - Finished Request
URL: about:blank
2018-04-30 09:39:56,980 - selenium.webdriver.remote.remote_connection - DEBUG - POST http://127.0.0.1:57672/wd/hub/session/0f971930-4c52-11e8-8957-cd8a79dc644c/element {"using": "id", "sessionId": "0f971930-4c52-11e8-8957-cd8a79dc644c", "value": "proceedButton"}
2018-04-30 09:39:57,278 - selenium.webdriver.remote.remote_connection - DEBUG - Finished Request
DebanjanB

要使用证书(.cer),同时连接到通过网站PhantomJS和Python,你需要提供的绝对路径--ssl-客户端证书文件C:\路径\到\ certificate.cerservice_args,你可以使用以下代码行:

driver = webdriver.PhantomJS(executable_path="Selenium/bin/phantomjs", service_args=['--ssl-client-certificate-file=C:\path\to\certificate.cer'], desired_capabilities=dcap)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

初始化Selenium WebDriver时如何解决python-Selenium错误“连接被拒绝”?

Python Selenium Chrome Web驱动程序

使用 Python 的 Selenium 网络驱动程序

Selenium:循环Web驱动程序(Python)

如何使用 selenium Web 驱动程序单击元素(Python)

如何打开新页面驱动程序python selenium

Python:Selenium 和 PhantomJS

Python selenium PhantomJS 代理

如何在python中的Selenium Chrome Web驱动程序中获取状态代码

如何在python中使用Selenium Web驱动程序获取文本

如何使用 Python Selenium

如何在Windows上使用Selenium和Python初始化AVG Secure Browser会话

Python:Selenium驱动程序find_elements_by_xpath:问题

无法保持对Selenium Python驱动程序中元素的关注

Selenium驱动程序中的Python增量字符串号

Python Selenium Chrome 驱动程序找不到元素

在 element selenium python chrome 驱动程序中查找元素

从python selenium web驱动程序中的文本裁剪整数

Python Selenium将密钥发送给驱动程序

Selenium Web 驱动程序 | 查找对象 | Python

“未定义驱动程序”Python/Selenium

在函数中调用驱动程序时,如何在 Python3 中保持 Selenium Webdriver 浏览器打开?

如何在Scrapy中使用Selenium驱动程序单击第二个链接(使用python)

Python3 Selenium Webdriver 在初始化期间超时

在 selenium、python 中上傳文件

使用python selenium下载文件,使用firefox驱动程序正确下载目录

Python selenium - 如何運行 Selenium Python Unittest

Python Selenium:如何打印链接?

Python Selenium:如何单击元素