硒脚本在python上完成后如何保持chrome浏览器窗口打开以重新使用

2023-07-05Python开发问题
23

本文介绍了硒脚本在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浏览器窗口打开以重新使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

在xarray中按单个维度的多个坐标分组
groupby multiple coords along a single dimension in xarray(在xarray中按单个维度的多个坐标分组)...
2024-08-22 Python开发问题
15

Pandas中的GROUP BY AND SUM不丢失列
Group by and Sum in Pandas without losing columns(Pandas中的GROUP BY AND SUM不丢失列)...
2024-08-22 Python开发问题
17

GROUP BY+新列+基于条件的前一行抓取值
Group by + New Column + Grab value former row based on conditionals(GROUP BY+新列+基于条件的前一行抓取值)...
2024-08-22 Python开发问题
18

PANDA中的Groupby算法和插值算法
Groupby and interpolate in Pandas(PANDA中的Groupby算法和插值算法)...
2024-08-22 Python开发问题
11

PANAS-基于列对行进行分组,并将NaN替换为非空值
Pandas - Group Rows based on a column and replace NaN with non-null values(PANAS-基于列对行进行分组,并将NaN替换为非空值)...
2024-08-22 Python开发问题
10

按10分钟间隔对 pandas 数据帧进行分组
Grouping pandas DataFrame by 10 minute intervals(按10分钟间隔对 pandas 数据帧进行分组)...
2024-08-22 Python开发问题
11