![](/img/trans.png)
[英]In Python, how to check if Selenium WebDriver has quit or not?
[英]Python Selenium: How to check whether the WebDriver did quit()?
我想控制我的WebDriver
是否退出但我找不到方法。 (好像Java里面有办法)
from selenium import webdriver
driver = webdriver.Firefox()
driver.quit()
driver # <selenium.webdriver.firefox.webdriver.WebDriver object at 0x108424850>
driver is None # False
我还探索了WebDriver
的属性,但找不到任何特定方法来获取有关驱动程序状态的信息。 同时检查 session id:
driver.session_id # u'7c171019-b24d-5a4d-84ef-9612856af71b'
如果您要探索 python-selenium 驱动程序的源代码,您会看到 firefox 驱动程序的quit()
方法在做什么:
def quit(self):
"""Quits the driver and close every associated window."""
try:
RemoteWebDriver.quit(self)
except (http_client.BadStatusLine, socket.error):
# Happens if Firefox shutsdown before we've read the response from
# the socket.
pass
self.binary.kill()
try:
shutil.rmtree(self.profile.path)
if self.profile.tempfolder is not None:
shutil.rmtree(self.profile.tempfolder)
except Exception as e:
print(str(e))
这里有一些事情你可以依赖:检查profile.path
是否存在或检查binary.process
状态。 它可以工作,但您也可以看到只有“外部调用”, python 端没有任何更改可以帮助您指示调用了quit()
。
换句话说,您需要进行外部调用以检查状态:
>>> from selenium.webdriver.remote.command import Command
>>> driver.execute(Command.STATUS)
{u'status': 0, u'name': u'getStatus', u'value': {u'os': {u'version': u'unknown', u'arch': u'x86_64', u'name': u'Darwin'}, u'build': {u'time': u'unknown', u'version': u'unknown', u'revision': u'unknown'}}}
>>> driver.quit()
>>> driver.execute(Command.STATUS)
Traceback (most recent call last):
...
socket.error: [Errno 61] Connection refused
你可以把它放在try/except
并创建一个可重用的函数:
import httplib
import socket
from selenium.webdriver.remote.command import Command
def get_status(driver):
try:
driver.execute(Command.STATUS)
return "Alive"
except (socket.error, httplib.CannotSendRequest):
return "Dead"
用法:
>>> driver = webdriver.Firefox()
>>> get_status(driver)
'Alive'
>>> driver.quit()
>>> get_status(driver)
'Dead'
另一种方法是制作您的自定义 Firefox webdriver并在quit()
中将session_id
设置为None
:
class MyFirefox(webdriver.Firefox):
def quit(self):
webdriver.Firefox.quit(self)
self.session_id = None
然后,您可以简单地检查session_id
值:
>>> driver = MyFirefox()
>>> print driver.session_id
u'69fe0923-0ba1-ee46-8293-2f849c932f43'
>>> driver.quit()
>>> print driver.session_id
None
我遇到了同样的问题并尝试返回标题 - 这对我使用 chromedriver 有用......
from selenium.common.exceptions import WebDriverException
try:
driver.title
print(True)
except WebDriverException:
print(False)
如何执行驱动程序命令并检查异常:
import httplib, socket
try:
driver.quit()
except httplib.CannotSendRequest:
print "Driver did not terminate"
except socket.error:
print "Driver did not terminate"
else:
print "Driver terminated"
以上建议的方法在硒版本 3.141.0 上对我不起作用
dir(driver.service) found a few useful options
driver.session_id
driver.service.process
driver.service.assert_process_still_running()
driver.service.assert_process_still_running
当我在关闭已经关闭的驱动程序时遇到问题时,我发现了这个 SO 问题,在我的情况下,围绕 driver.close() 的 try catch 为我的目的工作。
try:
driver.close()
print("driver successfully closed.")
except Exception as e:
print("driver already closed.")
还:
import selenium
help(selenium)
检查硒版本号
它在java中工作,在FF上检查
((RemoteWebDriver)driver).getSessionId() == null
有这个功能:
if driver.service.isconnectible(): 打印('好去')
''''python def closedriver(): global drivername drivername.quit() global driveron driveron=False ''''这个函数"closedriver"使用了一个名为"drivername"的全局变量和一个名为"driveron"的全局bool变量,你可以只需将当前驱动程序作为参数传递,但
注意: driveron 必须是全局的以存储驱动程序的状态为“on”或“off”。 ''''Python
定义关闭驱动程序(驱动程序名称):
全局驱动
尝试:
drivername.quit()
除了:
经过
驱动程序=假
''''
当您启动新驱动程序时,只需使用检查
全局驱动
如果不是驱动程序:
驱动程序=网络驱动程序。Chrome()
这对我有用:
from selenium import webdriver
driver = webdriver.Chrome()
print( driver.service.is_connectable()) # print True
driver.quit()
print( driver.service.is_connectable()) # print False
在我的例子中,我需要检测浏览器界面是否关闭 - 不管 - chromedriver 的状态。 因此,这里的答案都没有用,我只是决定 go 显而易见:
from selenium.common.exceptions import WebDriverException
try:
driver.current_url
print('Selenium is running')
except WebDriverException:
print('Selenium was closed')
尽管有点 hack,但它完美地达到了我的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.