Webdriver + PhantomJS就在那里

时间:2013-08-14 19:50:27

标签: python python-2.7 selenium selenium-webdriver phantomjs

我正在使用Selenium Webdriver(在Python中)自动从某个网站下载数千个文件(不能通过urllib,httplib等传统方式进行网页编写)。我的脚本与Firefox完美配合,但我不需要看到魔法发生,所以我正在尝试使用PhantomJS。它几乎一直工作,除非它试图单击某个按钮以关闭窗口。这是脚本卡住的命令:

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

它只是挂在那里,没有任何反应。

PhantomJS比Firefox更快(因为没有视觉效果),所以我认为问题可能与“关闭窗口”按钮不能很快点击有关。因此我尝试使用显式等待:

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

不起作用:等待很快结束(大约一秒钟后出现“等待完成”消息),但代码再次挂起。我也试过使用隐式等待,但这也不起作用。

所以,我很茫然。当我使用Firefox时,同样的脚本就像魅力一样运行,那么为什么它不适用于PhantomJS呢?

我不知道这是否有帮助,但这是页面来源:

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

我不知道这是否有帮助,但是当我用Crtl-C打破执行时,我明白了:

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

我是编程新手,我无法理解这个输出(我甚至不知道“插座”是什么)。但也许你们中的一些人可以指出我正确的方向?快速修复可能要求太多,但可能暗示可能会发生什么?

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

1 个答案:

答案 0 :(得分:0)

在脚本中运行以下代码行,解决了这个问题。

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