Python Webdriver&x2B;幻影就挂在那里

Python Webdriver&x2B;幻影就挂在那里,python,python-2.7,selenium,selenium-webdriver,phantomjs,Python,Python 2.7,Selenium,Selenium Webdriver,Phantomjs,我正在使用SeleniumWebDriver(Python)来自动从某个网站下载数千个文件(这些文件不能通过传统的方式(如urllib、httplib等)进行Web垃圾处理)。我的脚本与Firefox完美配合,但我不需要看到神奇的效果,所以我尝试使用PhantomJS。它几乎一直工作,除非它试图单击某个按钮以关闭窗口。下面是脚本被卡住的命令: browser.find_element_by_css_selector("img[alt=\"Close Window\"]").click() 它只

我正在使用SeleniumWebDriver(Python)来自动从某个网站下载数千个文件(这些文件不能通过传统的方式(如urllib、httplib等)进行Web垃圾处理)。我的脚本与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()
不起作用:等待很快结束(“done with waiting”消息在大约一秒钟后出现),但代码再次挂起。我也尝试过使用隐式等待,但也不起作用

所以,我不知所措。当我使用Firefox时,同样的脚本运行起来很有魅力,那么为什么它不能与PhantomJS一起工作呢

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

我也不知道这是否有帮助,但当我中断执行w/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
回溯(最近一次呼叫最后一次):
文件“myscript.py”,第361行,在
myfunction(一些参数,一些其他参数)
myfunction中第277行的文件“myscript.py”
浏览器。通过“css”选择器(“img[alt=\“关闭窗口\]”)查找元素。单击()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py”,第54行,单击
self.\u执行(命令。单击\u元素)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webelement.py”,第228行,在
返回self.\u parent.execute(命令,参数)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”,执行中第163行
响应=self.command\u executor.execute(driver\u command,params)
文件“/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”,执行中第349行
返回self.\u请求(url,方法=命令\u信息[0],数据=数据)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site packages/selenium-2.33.0-py2.7.egg/selenium/webdriver/remote/remote\u connection.py”,第396行,请求中
响应=opener.open(请求)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第404行,打开
响应=自身打开(请求,数据)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第422行,打开
"开放",
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第382行,在调用链中
结果=func(*args)
http_open中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第1214行
返回self.do_open(httplib.HTTPConnection,req)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py”,第1187行,在do_open中
r=h.getresponse(缓冲=True)
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第1045行,在getresponse中
response.begin()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第409行,在begin中
版本、状态、原因=self.\u读取\u状态()
文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py”,第365行,处于“读取”状态
line=self.fp.readline(_MAXLINE+1)
readline中的文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py”,第476行
数据=self.\u sock.recv(self.\r bufsize)
键盘中断
我是编程新手,我不能理解这个输出(我甚至不知道“套接字”是什么)。但也许你们中的一些人能给我指出正确的方向?一个快速解决方案可能要求太多,但可能是一个提示,可能会发生什么


(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');");

如果你跑这条线会怎么样执行脚本(“closeWindow(false),/lnacui2api/cart/displayCart.do”,“false”);我最终求助于快速而肮脏的
浏览器。close()
修复程序。现在我要到星期一才能访问代码,但是我会试试你的建议,看看我得到了什么输出。它是有效的!非常感谢,@ewyyntomotohm,在这种情况下,这个问题的主要解决方案可能与屏幕上图像的可见性有关。您可能希望执行诸如“window.scrollTo(0,[该图像应该可见的地方]);”之类的javascript,然后再次尝试查找该元素。我试试看。