我想控制我的WebDriver
是否退出,但我找不到方法。 (It seems that in Java there's a way to do it)
from selenium import webdriver
driver = webdriver.Firefox()
driver.quit()
driver # <selenium.webdriver.firefox.webdriver.WebDriver object at 0x108424850>
driver is None # False
我还探讨了WebDriver
的属性,但我无法找到任何特定方法来获取有关驱动程序状态的信息。还要检查会话ID:
driver.session_id # u'7c171019-b24d-5a4d-84ef-9612856af71b'
答案 0 :(得分:9)
如果你要探索python-selenium驱动程序的源代码,你会看到firefox驱动程序的quit()
method正在做什么:
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 并在session_id
中将None
设置为quit()
:
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
答案 1 :(得分:5)
我遇到了同样的问题并尝试返回标题 - 这对我使用chromedriver ...
from selenium.common.exceptions import WebDriverException
try:
driver.title
print(True)
except WebDriverException:
print(False)
答案 2 :(得分:1)
上面建议的方法在硒版本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
当我关闭已经关闭的驱动程序时遇到问题时,我发现了这个问题,在我的情况下,尝试绕过driver.close()可以达到我的目的。
try:
driver.close()
print("driver successfully closed.")
except Exception as e:
print("driver already closed.")
还:
import selenium
help(selenium)
检查硒的版本号
答案 3 :(得分:0)
如何执行驱动程序命令并检查异常:
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"
答案 4 :(得分:0)
它适用于java,在FF上检查
((RemoteWebDriver)driver).getSessionId() == null
答案 5 :(得分:0)
有此功能:
如果driver.service.isconnectible(): 打印(“好走了”)
答案 6 :(得分:0)
def closedriver():
global drivername
drivername.quit()
global driveron
driveron=False
答案 7 :(得分:0)
这对我有用:
from selenium import webdriver
driver = webdriver.Chrome()
print( driver.service.is_connectable()) # print True
driver.quit()
print( driver.service.is_connectable()) # print False