当URL错误时,PhantomJS不会返回错误(python)

时间:2017-12-13 23:19:31

标签: python linux selenium-webdriver error-handling phantomjs

我在python中使用selenium,尤其是PhantomJS 问题是当我发送错误的URL时,不会发生错误 而firefox驱动程序捕获错误

from selenium import webdriver
from selenium.common.exceptions import TimeoutException, 
NoSuchElementException
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.PhantomJS()
driver.get("drr,.gh")

我想知道我的程序如何识别虚假网址

这是我在这里的第一篇文章,所以如果我犯了错误,我道歉,我也为我的英语道歉

谢谢

1 个答案:

答案 0 :(得分:0)

当我们通过 Malformed URL 提供 get() 作为 GeckoDriver 方法的参数时 ChromeDriver 我们确实看到一个正确的异常显示为:

selenium.common.exceptions.WebDriverException: Message: Malformed URL: drr,.gh is not a valid URL.

但是当通过 Malformed URL get() 方法提供 PhantomJSDriver 作为参数时日志似乎缺少正确格式URL的验证, PhantomJSDriver 继续尝试浏览它永远不会成功的URL,如下所示:

[INFO  - 2017-12-14T08:13:37.981Z] GhostDriver - Main - running on port 2585
[INFO  - 2017-12-14T08:13:39.482Z] Session [b0debdb0-e0a6-11e7-ad4b-79a57b4a1a11] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.1.1 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2017-12-14T08:13:39.482Z] Session [b0debdb0-e0a6-11e7-ad4b-79a57b4a1a11] - page.customHeaders:  - {}
[INFO  - 2017-12-14T08:13:39.482Z] Session [b0debdb0-e0a6-11e7-ad4b-79a57b4a1a11] - 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,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2017-12-14T08:13:39.482Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: b0debdb0-e0a6-11e7-ad4b-79a57b4a1a11

解决方案:

作为一种解决方案,我们可以将 Malformed URL 作为 get() 方法的参数传递为 {{1您可以按如下方式引导 Unreachable Destination

set_page_load_timeout(seconds)

如果发生 from selenium import webdriver driver = webdriver.PhantomJS(executable_path=r'C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe') driver.set_page_load_timeout(2) driver.get("drr,.gh") ,您会看到以下日志消息:

page_load_timeout

您可以在此处找到有关set_page_load_timeout()方法的详细讨论。