python - Webdriver + PhantomJS 只是卡在那里

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

不起作用:WAITING很快结束(“等待完成”消息在一秒左右后出现),但随后代码再次挂起。我也尝试过使用隐式等待,但这也没有用。

所以,我很茫然。当我使用 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)

最佳答案

在您的脚本中运行以下代码行,问题就解决了。

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

关于python - Webdriver + PhantomJS 只是卡在那里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18240988/

相关文章:

python - python 中奇怪的静默崩溃与 : OSX, 请求、sqlite3、多处理的组合

python - python 中的 Opencv 2.4.3 estimateRigidTransform

java - 如何使用 xpath 获取遵循特定部分中的模式的链接?

java - 如何从 Selenium 获取 JavaScript 值?

python - 如何选择带有右箭头的链接作为 xpath 的文本?

python - 在python中将符号表达式转换为数组

python - clojure 使用 scipy 和 numpy

python - 将 Python 与 Twitter API 结合使用来发布推文?

python - 如何在python中对记录进行排序?

javascript - 测试 Extjs 网站时出现 Selenium 错误