Phantom JS Driver Cannot Find Items Sometimes

I am new to PhantomJS and I am trying to run selenium (python) tests using the phantomjs driver, but it will not be web elements.

Ghostdriver Logs:

[INFO - 2015-02-27T15:24:40.236Z] GhostDriver - Main - running on port 52653 [INFO - 2015-02-27T15:24:41.075Z] Session [bfd397f0-be94-11e4-ad03-b711254501c8] - 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.0.0 Safari/538.1","webSecurityEnabled":true} [INFO - 2015-02-27T15:24:41.075Z] Session [bfd397f0-be94-11e4-ad03-b711254501c8] - page.customHeaders: - {} [INFO - 2015-02-27T15:24:41.075Z] Session [bfd397f0-be94-11e4-ad03-b711254501c8] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.0.0","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"mac-10.9 (Mavericks)-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-02-27T15:24:41.075Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: bfd397f0-be94-11e4-ad03-b711254501c8 [ERROR - 2015-02-27T15:24:47.242Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1425050687190 :262 in error 

The intriguing part is that after I successfully installed phantomjs, I ran my login test and it passed without problems. Then I performed another test, which failed for the same reason that was mentioned above. I tried to run the Login test, which passed, but the phantomjs driver will no longer find the elements.

Any idea what causes this?

By the way, these tests work fine with chrome and FF

+6
source share
3 answers

So ... it looks like any element using Selenium WebDriver Wait actually uses a polling scheme. Remember that explicitly waiting for Selenium is the code that you define in order to wait for a certain condition before continuing with the code. WebDriverWait by default calls ExpectedCondition every 500 milliseconds until it returns successfully. ( link. ). What does this mean every 500 ms, Selenium checks the wait state. If this is true, go on. If this is not the case, wait for another polling frequency cycle, and then try again.

And from my testing, I believe that the survey is not ready yet, but let's call it an error that definitely generates errors in my ghostdriver.log

 [ERROR - 2016-08-14T08:50:12.896Z] WebElementLocator - _handleLocateCommand - Element(s) NOT Found: GAVE UP. Search Stop Time: 1471164612878 

I am working on a project that removes a complex single-page AJAX site. Since the site reuses common div elements, I need to make many calls (clear, paste the value, click, wait, go to another table on the page, copy the data of interest, repeat ...) to get the data I need. And since the site is slow, I apply the wait elements through:

 driver.wait.until(EC.presence_of_element_located((By.XPATH, "//*[@id='special_content_id']//td[contains(.,'" + info.unitId + "')]"))) 

It turns out that the polling frequency is set in the class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

Link here.

I was curious about the behavior. When my poll_frequency was set to 0.5 seconds, I got 98 WebElementLocator errors. When I switched this to 7.5 seconds, driver.wait = WebDriverWait(driver, 60, 7.5) , I received only 36 errors. Less polling time causes more errors.

What I really find strange is that I would not expect the usual polling behavior (Selenium) to result in a log error anywhere (PhantomJS). I'm sorry that there was no other journal entry for the survey, the item is not ready yet ...

+3
source

I noticed that these elements are not in the test cases using find_element methods using the phantomJS driver, which can be accessed by javascript using the JavascriptExecutor. Use java script to access the element and to act on the element. I hope this solves your problem.

0
source

Perhaps your selenium test is trying to find the item before loading it. After the first unsuccessful attempt, you should try again and again and continue until you find that the item or timeout has been exceeded.

Take a look at splinter https://splinter.readthedocs.org/en/latest/index.html It works with selenium and implements the logic of waiting for elements.

0
source

Source: https://habr.com/ru/post/983111/


All Articles