6

我正在使用 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)

4

1 回答 1

0

Running the following line of code in your script, solves the question.

browser.execute_script("closeWindow(false, '/lnacui2api/cart/displayCart.do', 'false');");
于 2015-12-18T00:35:43.253 回答