Python Selenium PhantomJS在单击元素后返回空源

时间:2015-11-07 21:01:50

标签: javascript python html selenium phantomjs

See the code

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get("http://www.metasozluk.com/?r=girdi/goster&g=298794")
driver.page_source
# Returns a whole bunch of HTML page.

driver.find_element_by_css_selector(".entry-list-cats-link").click()
driver.page_source
# Returns '<html><head></head><body></body></html>'

当我在PhantomJS上使用Python的Selenium加载页面时,页面加载良好。但是,当我尝试操作DOM元素时,它只是将整个源代码更改为<html><head></head><body></body></html>

此问题在Firefox中也不会发生,但我需要一个无头浏览器,似乎没有其他替代方案而不是PhantomJS。这是一个错误吗?

编辑:找到一些日志

我在与脚本相同的路径上找到了日志名ghostdriver.log。就是这样:

PhantomJS is launching GhostDriver...
[INFO  - 2015-11-07T20:45:38.121Z] GhostDriver - Main - running on port 51516
[INFO  - 2015-11-07T20:45:39.090Z] Session [8101ecc0-8590-11e5-91d2-17516fa65f1d] - CONSTRUCTOR - Desired Capabilities: {"platform":"ANY","version":"","javascriptEnabled":true,"browserName":"phantomjs"}
[INFO  - 2015-11-07T20:45:39.090Z] Session [8101ecc0-8590-11e5-91d2-17516fa65f1d] - CONSTRUCTOR - Negotiated Capabilities: {"browserName":"phantomjs","version":"1.9.0","driverName":"ghostdriver","driverVersion":"1.0.3","platform":"linux-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"}}
[INFO  - 2015-11-07T20:45:39.091Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 8101ecc0-8590-11e5-91d2-17516fa65f1d
[INFO  - 2015-11-07T20:50:38.119Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW
[INFO  - 2015-11-07T20:55:38.213Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW
[INFO  - 2015-11-07T21:00:38.311Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW
[INFO  - 2015-11-07T21:05:38.330Z] SessionManagerReqHand - _cleanupWindowlessSessions - Asynchronous Sessions clean-up phase starting NOW

我不确定_cleanupWindowlessSessions部分,也许这是关于问题的线索。

编辑: - signore-ssl-errors = true无法解决问题

我做了进一步的编码,但是在忽略ssl错误的同时遇到了同样的问题。也有一些答案说,忽略SSL错误可以解决问题,但似乎没有。所以我为了遇到这个问题而取消了this answer's validity

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题,很多人都在报告忽略ssl问题解决它,所以它可能对你有用。

self.driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])