1

Is there any way to switch Chrome webdriver from headless mode to window mode?

One thing that came to my mind is to 'switch' existing web driver to non-headless mode. Another idea: to create new instance of webdriver (this time non-headless) with some sort of 'state' from old one so the user operations can be executed. I don't know how to do or if it is possible though.

import os
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException,

options = webdriver.ChromeOptions()
options.add_argument('headless')
driver = webdriver.Chrome(
    executable_path=os.path.join(os.getcwd(), 'chromedriver'),
    chrome_options=options,
    )
driver.get('https://website.com')
try:
    driver.find_element_by_xpath('//h1').click()
except NoSuchElementException:
    print('You have to click it manually')
    # here I need Chrome browser
    # to be opened so that I can click a link
print('The name of this thing is: ', end='')
print(driver.find_element_by_xpath("//h1[@class='name']").text)
Mateusz
  • 19
  • 1
  • 3
  • why do you need window mode ? i mean what do you want with window mode that you think that you are not able to do this otherwise – parik Mar 06 '18 at 15:36
  • e.g I need window mode to click the link if the given one was not found, etc – Mateusz Mar 06 '18 at 15:48
  • if you can click on the link manually, you can do this with your spider – parik Mar 06 '18 at 15:54
  • I need user to whom I write this to be able to recognize what happened and to react 'manually'. – Mateusz Mar 06 '18 at 15:59
  • 2
    Just start the driver as headed to begin with. There is no way to switch after the driver has been launched. Creating a new driver and copying the state *may* be possible, but depends on what the website uses to maintain the session (and if you need the session maintained at all). But the obvious solution is just to use a headed browser to start with. – sytech Mar 06 '18 at 18:16

1 Answers1

1

If you need to open a new tab

driver.execute_script("window.open()")

If you need to switch to this new one

driver.switch_to.window(self.driver.window_handles[1])

Then you get the page

driver.get('https://website.com')

and the end you can close it (the new one)

driver.close()

and you back to the first driver

switch_to.window(driver.window_handles[0])
parik
  • 2,313
  • 12
  • 39
  • 67