如何使无头浏览器可见的Python [英] How to make headless browser visible Python

查看:227
本文介绍了如何使无头浏览器可见的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过设置以下参数创建了无头的webdriver chrome浏览器:

 chrome_options.add_argument("--headless")

,然后使用以下命令打开浏览器:

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)

一旦满足条件,是否可以使浏览器出现?我尝试使用以下方法再次删除该属性:

chrome_options.arguments.remove("--headless")

但是那什么也没做.

解决方案

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

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

一种更简洁的方法是在tearDown(){}方法中调用 driver.quit() 关闭销毁当前的 ChromeDriver Chrome浏览器实例,然后使用新的配置集覆盖一组新的 ChromeDriver Chrome浏览器实例.


参考文献

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

I have created a headless webdriver chrome browser by setting this argument:

 chrome_options.add_argument("--headless")

and then opening the browser using:

driver = webdriver.Chrome(executable_path=os.path.abspath("chromedriver"), chrome_options=chrome_options)

Is it possible to make the browser appear once a condition is met? I have tried removing the attribute again using:

chrome_options.arguments.remove("--headless")

but that does not do anything.

解决方案

As you have initiated a --headless instance by setting:

chrome_options.add_argument("--headless")

further, it won't be possible to make the Browsing Context visible again within the same session.


Reason

When you configure ChromeDriver using ChromeOptions() to initiate 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.

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.

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.


References

You can find a couple of relevant discussions in:

这篇关于如何使无头浏览器可见的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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