Selenium:在运行代码时禁用无头吗?(Python) [英] Selenium: Disable headless when code is running? (Python)

查看:70
本文介绍了Selenium:在运行代码时禁用无头吗?(Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了这篇文章:

如何使无头浏览器可见Python

但是显然,一旦代码运行,就不可能禁用无头,还有其他选择吗?

But apparently it's not possible to disable headless once the code is running, is there an alternative?

我的代码检测页面上是否存在验证码,显示窗口,并且一旦完成验证码,它就会再次隐藏该窗口.

My code detects if there is a captcha on the page, displays the window and once the captcha is done it hide again the window.

推荐答案

,将无法初始化 headless ,然后使其可见以解决

No, it won't be possible to initialize google-chrome headlessly and then make it visible to solve the captcha.

当您配置 ChromeDriver 使用 ChromeOptions()发起在启动新的 Chrome浏览器会话的过程中,配置被烘焙 chromedriver 可执行文件中,并将持续到 WebDriver 的生命周期,并保持不可修改.因此,您可以修改当前正在执行的 WebDriver 实例的 ChromeOptions .

When you configure ChromeDriver using ChromeOptions() to initiate headless in the process of initiating a new Chrome Browsing Session the configuration gets baked into the chromedriver executable and will persist till the lifetime of the WebDriver and remains uneditable. So you modify the ChromeOptions of the WebDriver instance which is currently in execution.

即使您能够提取 ChromeDriver ChromeSession 属性,例如来自已启动的 ChromeDriver Chrome浏览器会话会话ID 和其他会话属性,您仍然不会能够更改 ChromeDriver 的属性集.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies and other session attributes from the already initiated ChromeDriver and Chrome Browsing Session still you won't be able to change the set of attributes of the ChromeDriver.

一种更干净的方法是在 tearDown(){} 方法中调用 driver.quit() 关闭销毁当前的 ChromeDriver Chrome浏览器实例,然后跨越一组新的 ChromeDriver 和<具有新配置的em> Chrome浏览器实例.

A cleaner way would be to call driver.quit() within tearDown(){} method to close and destroy the current ChromeDriver and Chrome Browser instances gracefully and then span a new set of ChromeDriver and Chrome Browser instance with the new set of configurations.

您可以在以下位置找到一些相关的讨论:

You can find a couple of relevant discussions in:

这篇关于Selenium:在运行代码时禁用无头吗?(Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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