Python Selenium:如何检查WebDriver是否已退出()? [英] Python Selenium: How to check whether the WebDriver did quit()?

查看:1115
本文介绍了Python Selenium:如何检查WebDriver是否已退出()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制我的WebDriver是否退出,但是我找不到用于该退出的方法. (似乎在Java中,有一种方法可以做到这一点)

I want to control whether my WebDriver quit but I can't find a method for that. (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:

I also explored the attributes of WebDriver but I can't locate any specific method to get information on the driver status. Also checking the session id:

driver.session_id # u'7c171019-b24d-5a4d-84ef-9612856af71b'

推荐答案

如果您要探索python-selenium驱动程序的源代码,您会看到

If you would explore the source code of the python-selenium driver, you would see what the quit() method of the firefox driver is doing:

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是否存在或检查,这可以帮助您指示已调用quit().

There are things you can rely on here: checking for the profile.path to exist or checking the binary.process status. It could work, but you can also see that there are only "external calls" and there is nothing changing on the python-side that would help you indicate that quit() was called.

换句话说,您需要拨打外部电话以检查状态:

In other words, you need to make an external call to check the status:

>>> 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下,并使其具有可重用的功能:

You can put it under the try/except and make a reusable function:

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网络驱动程序,然后在quit()中将session_id设置为None:


Another approach would be to make your custom Firefox webdriver and set the session_id to None in 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

这篇关于Python Selenium:如何检查WebDriver是否已退出()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆