如何在同一会话中将 selenium webdriver 从无头模式设置为正常模式? [英] How to set selenium webdriver from headless mode to normal mode within the same session?

本文介绍了如何在同一会话中将 selenium webdriver 从无头模式设置为正常模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在将 selenium webdriver 设置为无头模式后将其设置回正常模式?

Is it possible after setting selenium webdriver to a headless mode set it back to a normal mode?

from selenium import webdriver
from selenium.webdriver.firefox.options import Options

options = Options()
options.headless = True
driver = webdriver.Firefox(options=options)
driver.get(http://stackoverflow.com)

# set driver back to normal mode

推荐答案

,将无法让 Chrome 最初以 headless 模式运行然后切换在同一会话中返回正常模式.

No, it won't be possible to make Chrome operate initially in headless mode and then switch back to normal mode within the same session.

当您使用 ChromeOptions() 配置 ChromeDriver 的实例以跨越新的 Chrome 浏览上下文时,配置会烘焙chromedriver 可执行文件中,它将在 WebDriver 的生命周期内持续存在并且不可编辑.因此,您无法通过 ChromeOptions() 类修改/添加任何现有/新的配置到当前正在执行的 WebDriver 实例.

When you configure an instance of a ChromeDriver with ChromeOptions() to span a new Chrome Browsing Context the configuration gets baked within the chromedriver executable which will persist for the lifetime of the WebDriver and being uneditable. So you can't modify/add any existing/new configuration through ChromeOptions() class to the WebDriver instance which is currently in execution.

即使您能够提取 ChromeDriverChromeSession 属性,例如会话 IDCookiesUserAgent 和其他已启动的 ChromeDriverChrome 浏览会话 的会话属性仍然无法更改 ChromeDriver 的属性集.

Even if you are able to extract the ChromeDriver and ChromeSession attributes e.g. Session ID, Cookies, UserAgent 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()关闭销毁当前的 ChromeDriverChrome Browser 实例,然后跨越一组新的 ChromeDriverChrome 浏览器具有新配置集的实例.

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 webdriver 从无头模式设置为正常模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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