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

Python Selenium - How to configure a client certificate file while initializing PhantomJS driver instance

我需要使用证书 (.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

要在通过 PhantomJS 和 Python 连接到网站时使用证书 (.cer),您需要提供 --ssl-client-certificate-file 例如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)