Headless Chrome browser won't switch to next window (Selenium + Python)

  • QA Engineer

    So I have a test case: When clicking a button a new window is opened. To switch to that new window I use:

    window_after = self.driver.window_handles[1]

    When I execute the test case in normal Chrome browser, there is no error and I receive a positive outcome. But when I use headless Chrome, I'm getting an error: "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//h1[text()='Trip Terms and Conditions']" (Session info: headless chrome=78.0.3904.87)" This is how I use the headless Chrome:

    chrome_options = Options()
    driver = webdriver.Chrome(options=chrome_options)

    So as I understand, headless Chrome can't switch to the new opened window. Is there a error in the code? What I'm doing wrong?
    Thank you.

  • I don't know the exact answer but maybe can help find the solution. Have you tried finding out if this is actually the correct window handle? You could try debugging by:

    • Counting the number of window handles
    • Opening the last window handle in stead of exactly [1]
    • Reading the DOM of all window handles to see what is found by executing this script over each handle and writing to console or something driver.execute_script("return document.documentElement.outerHTML")

Suggested Topics