0

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]
self.driver.switch_to.window(window_after)

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()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
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.

  • Do you wait for the element? Maybe the switch is too fast headless? – Niels van Reijmersdal Nov 1 at 12:15
  • Yes, sure. Same result. – rodut Nov 2 at 10:21
0

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")
  • As I wrote before, when I execute the test case in normal Chrome browser, there is no error and I receive a positive outcome. Thank you. – rodut Nov 2 at 10:25
  • Yes I read your post. Have you tried debugging to find out if headless tries to find the element in the correct window/tab ? Maybe there is a bug in headless mode. – Jeroen Lamberts Nov 4 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.