How to set selenium webdriver from headless mode to normal mode within the same session?

痴心易碎 提交于 2019-12-18 08:58:47

问题


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

回答1:


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.

When you configure an instance of a ChromeDriver with ChromeOptions to span a new Chrome Browsing Session the configuration of the ChromeDriver remains unchanged throughout the lifetime of the ChromeDriver and remains uneditable. So you can't modufy/add any existing/new configuration through ChromeOptions class to the WebDriver instance any more which is currently in execution.

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.

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 relevant discussion in How can I reconnect to the browser opened by webdriver with selenium?



来源:https://stackoverflow.com/questions/54721676/how-to-set-selenium-webdriver-from-headless-mode-to-normal-mode-within-the-same

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!