0

I am using selenium in python, especially PhantomJS the problem is when I send a wrong URL, no error happens while firefox driver catch an error

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

I want to know how my program can recognize a false URL

This is my first post here so I apologize if I made an error, I also apologize for my English

Thank you

  • It is unclear what you are asking. You mean that want to recognize a wrong URL even before the driver tries to navigate to it? Or do you expect an exception but not getting one? – Eugene S Dec 14 '17 at 03:21
  • I am expecting an exception, I want my script to throw an exception when the driver is handling a wrong URL – Tayeb SAADI Dec 14 '17 at 20:16

1 Answers1

0

When we provide a Malformed URL as an argument to get() method through GeckoDriver or ChromeDriver we do see a proper exception shows as :

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

But when providing the Malformed URL as an argument to get() method through PhantomJSDriver from the logs it seems the validation of a proper formatted URL is missing and PhantomJSDriver keeps on attempting to browse to the URL which it never succeeds which is as follows :

[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

Solution :

As a solution, we can generalize the passing of Malformed URL as an argument to get() method as an Unreachable Destination and you can induce the set_page_load_timeout(seconds) as follows :

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

In the event of page_load_timeout occuring you would see the following log messages :

[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

You can find a detailed discussion on set_page_load_timeout() method here.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thank you for your answer and for the information, but it didn't solve my problem, I noticed that when I give a wrong URL, the driver doesn't take a long time with it, actually, with this solution, some legit URLs have errors now, especially the ones that take time to completely load – Tayeb SAADI Dec 14 '17 at 20:26
  • So your Question, **PhantomJS is not returning errors (python)** is solved now. Please **`Accept`** the **`Answer`** by clicking on the tick mark beside my answer, just below VoteDown arrow so the tick mark turns reen. But again, each application needs some `Benchmarks`, e.g. **page_load_time**. `Page Loading` shouldn't be a never ending process. Configure a `time_out` for the Page Loading process e.g. **driver.set_page_load_timeout(30)**. The working one will load whilest the Malformed one will time_out. See the link to the detailed discussion within the Answer. – undetected Selenium Dec 15 '17 at 07:51
  • **set_page_load_timeout** doesn't solve my problem. Because the driver handles the bad URL so quickly, I am not sure but it seems like the driver just ignores it. As for my question I must admit that I didn't choose the right title, it's not that Phantom doesn't return errors, but rather it doesn't return the error that I want so you didn't solve my problem ^^, it is my mistake and I apologize for the misunderstanding – Tayeb SAADI Dec 15 '17 at 16:55