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)
解决方案
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])