如何在同一会话中将selenium webdriver从无头模式设置为正常模式?

问题描述 投票:2回答:1

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

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
python selenium-chromedriver selenium-firefoxdriver google-chrome-headless firefox-headless
1个回答
2
投票

不,将无法使Chrome最初在无头模式下运行,然后在同一会话中切换回正常模式。

当您使用ChromeOptions配置ChromeDriver实例以跨越新的Chrome浏览会话时,ChromeDriver的整个生命周期内ChromeDriver的配置保持不变,并且仍然无法编辑。因此,您无法通过ChromeOptions类将任何现有/新配置模块化/添加到当前正在执行的WebDriver实例中。

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

更简洁的方法是在driver.quit()方法中调用tearDown(){}来优雅地关闭和销毁当前的ChromeDriver和Chrome浏览器实例,然后使用新的配置跨越一组新的ChromeDriver和Chrome浏览器实例。

您可以在How can I reconnect to the browser opened by webdriver with selenium?找到相关的讨论

© www.soinside.com 2019 - 2024. All rights reserved.