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

时间:2018-04-27 10:46:23

标签: python selenium selenium-webdriver webdriver phantomjs

我需要使用证书(.cer)连接到网站。我在Python中使用PhantomJS。这是连接代码:

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>

,网址为:

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

1 个答案:

答案 0 :(得分:1)

要在通过 PhantomJS 和Python连接到网站时使用证书(.cer),您需要提供 - ssl-client-certificate-file <的绝对路径/ strong>例如 C:\ path \ to \ certificate.cer 作为service_args,您可以使用以下代码行:

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