<p>这种行为的原因是PhantomJS驱动程序的<a href="https://code.google.com/p/selenium/source/browse/py/selenium/webdriver/phantomjs/service.py" rel="nofollow noreferrer">^{<cd1>} class</a>是如何实现的。</p>
<p>定义了一个<code>__del__</code>方法来调用<code>self.stop()</code>方法:</p>
<pre><code>def __del__(self):
# subprocess.Popen doesn't send signal on __del__;
# we have to try to stop the launched process.
self.stop()
</code></pre>
<p>并且,<code>self.stop()</code>假设服务实例在尝试访问其属性时仍处于活动状态:</p>
<pre><code>def stop(self):
"""
Cleans up the process
"""
if self._log:
self._log.close()
self._log = None
#If its dead dont worry
if self.process is None:
return
...
</code></pre>
<p>同样的问题在本线程中得到了完美的描述:</p>
<ul>
<li><a href="https://stackoverflow.com/questions/18058730/python-attributeerror-on-del">Python attributeError on __del__</a></li>
</ul>
<hr/>
<p>您应该做的是静默地忽略退出驱动程序实例时发生的<code>AttributeError</code>:</p>
<pre><code>try:
driver.quit()
except AttributeError:
pass
</code></pre>
<p>这个问题是由这个<a href="https://code.google.com/p/selenium/issues/detail?id=7304" rel="nofollow noreferrer">revision</a>引起的。这意味着降级到<code>2.40.0</code>也会有帮助。</p>