python无头浏览器截图,如何使无头浏览器可见的Python

当使用Python的Selenium库创建了一个无头Chrome浏览器后,无法在同一个会话中让它重新显示。原因在于,一旦设置为无头模式,配置将固定在WebDriver实例中,无法更改。解决办法是在需要时关闭当前浏览器实例,然后重新初始化一个新的带有不同配置(非无头模式)的浏览器实例。
摘要由CSDN通过智能技术生成

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 google-chrome 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 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.

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:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值