我想控制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
的属性,但找不到任何特定的方法来获取驱动程序状态的信息。
driver.session_id # u'7c171019-b24d-5a4d-84ef-9612856af71b'
9条答案
按热度按时间tsm1rwdh1#
如果您要研究python-selenium驱动程序的源代码,您将看到firefox驱动程序的
quit()
方法正在做什么:这里有您可以信赖的东西:检查
profile.path
是否存在或检查binary.process
的状态。它可以工作,但您也可以看到只有"外部调用",并且 * Python端没有任何变化 *,这将有助于您指示quit()
被调用。换句话说,您需要进行外部调用来检查 * status *:
你可以把它放在
try/except
下,并创建一个可重用的函数:用法:
另一种方法是 * 制作您的自定义Firefox web驱动程序 *,并在
quit()
中将session_id
设置为None
:然后,您可以简单地检查
session_id
值:dfuffjeb2#
我遇到了同样的问题,并试图返回标题-这对我使用Chromedriver有效...
3qpi33ja3#
以上建议的方法在Selenium版本3.141.0上不起作用
当我在关闭一个已经关闭的驱动程序时遇到问题时,我发现了这个问题,在我的例子中,一个围绕驱动程序的try catch。close()对我的目的很有效。
同时:
检查Selenium版本号
wooyq4lh4#
这对我的工作:
ffdz8vbo5#
如何执行驱动程序命令并检查异常:
vcirk6k66#
它在java中工作,在FF上检查
gcuhipw97#
有这样的功能:
如果驱动程序.服务.是可连接的():print('可以进行')
2fjabf4q8#
在我的例子中,我需要检测浏览器界面是否关闭-不管- chromedriver的状态。因此,这里的答案都不起作用,我只是决定去显而易见的:
虽然这是一个有点黑客,它已经达到了我的目的完美。
rks48beu9#
“”“Python定义关闭驱动程序():global drivername drivername.quit()global driveron driveron=False“''这个函数“closedriver”使用了一个名为“drivername”的全局变量和一个名为“driveron”的全局布尔变量,您可以只传递当前驱动程序作为参数,但是
注意:driveron必须是全局的,才能存储驱动程序的状态为“on”或“off”。''' python
定义关闭驱动程序(驱动程序名称):
全局驱动程序on
尝试:
驱动程序名称.quit()
但以下情况除外:
通过
driveron=假
''''
当你启动一个新的驱动程序时,只需使用一个
全局驱动程序on
如果驱动程序未打开:
驱动程序=网页驱动程序.Chrome()