【问题标题】:PhantomJS is not returning errors when URL is wrong (python)当 URL 错误时,PhantomJS 不返回错误(python)
【发布时间】:2017-12-13 23:19:30
【问题描述】:

我在 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")

我想知道我的程序如何识别错误的 URL

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

谢谢

【问题讨论】:

  • 不清楚你在问什么。您的意思是即使在驱动程序尝试导航到它之前就想识别错误的 URL?还是您希望出现异常但没有得到异常?
  • 我期待一个异常,我希望我的脚本在驱动程序处理错误的 URL 时抛出异常

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


【解决方案1】:

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

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

但是,当通过 PhantomJSDriver 从日志中将 Malformed URL 作为参数提供给 get() 方法时,似乎是验证缺少正确格式的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() 方法作为 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,您将看到以下日志消息:

[ERROR - 2017-12-14T08:25:43.994Z] RouterReqHand - _handle.error - {"name":"Missing Command Parameter","message":"{\"headers\":{\"Accept\":\"application/json\",\"Accept-Encoding\":\"identity\",\"Connection\":\"close\",\"Content-Length\":\"71\",\"Content-Type\":\"application/json;charset=UTF-8\",\"Host\":\"127.0.0.1:2650\",\"User-Agent\":\"Python http auth\"},\"httpVersion\":\"1.1\",\"method\":\"POST\",\"post\":\"{\\\"pageLoad\\\": 2000, \\\"sessionId\\\": \\\"60b5bc60-e0a8-11e7-bb6f-8df56dd28746\\\"}\",\"url\":\"/timeouts\",\"urlParsed\":{\"anchor\":\"\",\"query\":\"\",\"file\":\"timeouts\",\"directory\":\"/\",\"path\":\"/timeouts\",\"relative\":\"/timeouts\",\"port\":\"\",\"host\":\"\",\"password\":\"\",\"user\":\"\",\"userInfo\":\"\",\"authority\":\"\",\"protocol\":\"\",\"source\":\"/timeouts\",\"queryKey\":{},\"chunks\":[\"timeouts\"]},\"urlOriginal\":\"/session/60b5bc60-e0a8-11e7-bb6f-8df56dd28746/timeouts\"}","line":546,"sourceURL":"phantomjs://code/session_request_handler.js","stack":"_postTimeout@phantomjs://code/session_request_handler.js:546:73\n_handle@phantomjs://code/session_request_handler.js:148:25\n_reroute@phantomjs://code/request_handler.js:61:20\n_handle@phantomjs://code/router_request_handler.js:78:46"}

  phantomjs://platform/console++.js:263 in error

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

【讨论】:

  • 感谢您的回答和信息,但它并没有解决我的问题,我注意到当我提供错误的 URL 时,驱动程序实际上并没有花费很长时间,使用此解决方案,现在一些合法的 URL 会出现错误,尤其是那些需要时间才能完全加载的 URL
  • 所以你的问题,PhantomJS 没有返回错误(python) 现在已经解决了。请Accept Answer 点击我的答案旁边的刻度线,就在 VoteDown 箭头下方,这样刻度线就会重新显示。但同样,每个应用程序都需要一些 Benchmarks,例如page_load_timePage Loading 不应该是一个永无止境的过程。为页面加载过程配置time_out,例如driver.set_page_load_timeout(30)。工作的将加载,而格式错误的将超时。请参阅答案中详细讨论的链接。
  • set_page_load_timeout 不能解决我的问题。因为驱动程序处理错误 URL 的速度如此之快,所以我不确定,但似乎驱动程序只是忽略了它。至于我的问题我必须承认我没有选择正确的标题,不是Phantom不返回错误,而是它没有返回我想要的错误所以你没有解决我的问题^^,这是我的错误,我为误解道歉
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-14
相关资源
最近更新 更多