How to keep chrome browser window open to be re-used after selenium script finishes on python(硒脚本在python上完成后如何保持chrome浏览器窗口打开以重新使用)
问题描述
在 selenium 执行完我的测试脚本后,我试图让 chrome 浏览器保持打开状态.我想重复使用同一个窗口来运行我的第二个脚本.
I am trying to keep the chrome browser open after selenium finishes executing my test script. I want to re-use the same window for my second script to run.
推荐答案
当您的 Chrome webdriver 实例变量被垃圾回收时,浏览器窗口关闭.如果即使脚本完成执行也想避免这种情况,可以将其设为全局.即:
Browser window closes when your Chrome webdriver instance variable is garbage collected. If you want to avoid this even when your script finishes executing, you can make it global. I.e.:
def test():
global driver # this will prevent the driver variable from being garbage collected
driver = webdriver.Chrome()
...
说明:selenium.webdriver.Chrome 类实例包含 Service 类的实例.后者有一个 __del__ 方法,当实例在垃圾收集过程中被破坏时调用.该方法反过来会停止服务并导致 Chrome 浏览器窗口关闭.
Explanation:
A selenium.webdriver.Chrome class instance contains an instance of a Service class. The latter has a __del__ method which is called when the instance is being destructed during garbage collection process. The method in turn stops the service and causes Chrome browser window to close.
这也解释了为什么有些用户没有观察到这种行为.我怀疑这是因为他们在文件范围内有 Chrome webdriver 实例变量,而不是在函数内.
This also explains why some users don't observe this behavior. I suspect that this is because they have Chrome webdriver instance variable at file scope, not inside a function.
这篇关于硒脚本在python上完成后如何保持chrome浏览器窗口打开以重新使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:硒脚本在python上完成后如何保持chrome浏览器窗口打开以重新使用
基础教程推荐
- Kivy 使用 opencv.调整图像大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
