Webdriver + PhantomJS just hangs there

I use Selenium Webdriver (in Python) to automate the download of thousands of files from a specific website (which cannot be used using regular tools like urllib, httplib, etc.). My script works fine with Firefox, but I don't need to see the magic, so I'm trying to use PhantomJS. It works almost completely down, except when it tries to click a specific button to close the window. Here is the command the script comes into:

browser.find_element_by_css_selector("img[alt=\"Close Window\"]").click() 

It just hangs there, nothing happens.

PhantomJS is faster than Firefox (since there are no visual effects), so I thought the problem might be with the Close Window button, which would not be available for quick access. So I tried using an explicit wait:

 element = WebDriverWait(browser, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img[alt=\"Close Window\"]"))) print "done with waiting" browser.find_element_by_css_selector("img[alt=\"Close Window\"]").click() 

Doesn't work: the wait ends pretty quickly (the message "done with the wait" appears after a second or so), but then the code freezes again. I also tried using implicit wait, but that didn't work either.

So, I am at a loss. The same script works like a charm when I use Firefox, so why doesn't it work with PhantomJS?

I don't know if this helps, but here is the source of the page:

http://www.flickr.com/photos/ 88729961@N00 / 9512669916 / sizes / l / in / photostream /

I don't know if this helps, but when I break the w / Crtl-C execution, I get the following:

 Traceback (most recent call last): File "myscript.py", line 361, in <module> myfunction(some_argument, some_other_argument) File "myscript.py", line 277, in myfunction browser.find_element_by_css_selector("img[alt=\"Close Window\"]").click() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 54, in click self._execute(Command.CLICK_ELEMENT) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py", line 228, in _execute return self._parent.execute(command, params) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webdriver.py", line 163, in execute response = self.command_executor.execute(driver_command, params) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/remote_connection.py", line 349, in execute return self._request(url, method=command_info[0], data=data) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/remote_connection.py", line 396, in _request response = opener.open(request) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 404, in open response = self._open(req, data) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 422, in _open '_open', req) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 382, in _call_chain result = func(*args) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1214, in http_open return self.do_open(httplib.HTTPConnection, req) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 1187, in do_open r = h.getresponse(buffering=True) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1045, in getresponse response.begin() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 409, in begin version, status, reason = self._read_status() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 365, in _read_status line = self.fp.readline(_MAXLINE + 1) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline data = self._sock.recv(self._rbufsize) KeyboardInterrupt 

I am new to programming and I can’t understand this result (I don’t even know what a socket is). But maybe some of you can point me in the right direction? A quick fix might be too much to ask, but maybe a hint of what might be happening?

(Mac OS X 10.6.8, Python 2.7.5, Selenium 2.33, PhantomJS 1.9.1)

+6
source share
1 answer

Executing the following line of code in a script solves the issue.

 browser.execute_script("closeWindow(false, '/lnacui2api/cart/displayCart.do', 'false');"); 
0
source

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


All Articles