SeleniumHQ / selenium

A browser automation framework and ecosystem.
https://selenium.dev
Apache License 2.0
30.63k stars 8.18k forks source link

[🐛 Bug]: New window not opening in headless mode #12597

Closed iyamk closed 1 year ago

iyamk commented 1 year ago

What happened?

My code:

import sys, time, io, os
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.os_manager import ChromeType
from selenium_stealth import stealth
import threading

driver = None
thread_main = None

def runner():
    global driver
    op = webdriver.ChromeOptions()
    op.add_argument('headless')
    op.add_argument('start-maximized')
    op.add_argument('user-agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36')
    op.add_argument('user-data-dir=/home/user/.config/google-chrome')
    op.add_experimental_option('excludeSwitches', ['enable-automation'])
    op.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(
        service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()),
        options=op
    )
    stealth(driver,
        languages=['en-US', 'en'],
        vendor='Google Inc.',
        platform='Win32',
        webgl_vendor='Intel Inc.',
        renderer='Intel Iris OpenGL Engine',
        fix_hairline=True,
    )

    driver.get('https://biglike.org')

    btn_login = driver.find_element(By.CSS_SELECTOR, 'a[href="#login"]')
    btn_login.click()
    btn_login_vk = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'a[data-uloginbutton="vkontakte"]')))
    btn_login_vk.click()
    all_guid = driver.window_handles
    driver.switch_to.window(all_guid[1])
    continue_btn = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-test-id="continue-as-button"]')))
    print(driver.page_source)
    continue_btn.click()
    driver.switch_to.window(all_guid[0])
    print(driver.page_source)

if __name__ == '__main__':
    thread_main = threading.Thread(target=runner)
    thread_main.start()

How can we reproduce the issue?

Run code But for this you need to log in to VKontakte Then comment out op.add_argument('headless') And the code will work well and display the VK page code. But with the headless option, it displays the biglike.org site code and gives an error which means that it cannot switch the window in headless mode

Relevant log output

Error:

Exception in thread Thread-2 (runner):
Traceback (most recent call last):
  File "/usr/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.11/threading.py", line 975, in run
    self._target(*self._args, **self._kwargs)
  File "/tmp/app/main.py", line 50, in runner
    continue_btn = WebDriverWait(driver, 2).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'button[data-test-id="continue-as-button"]')))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/tmp/app/venv/lib/python3.11/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
#0 0x564aadb89e23 <unknown>
#1 0x564aad8b27a7 <unknown>
#2 0x564aad8f11d3 <unknown>
#3 0x564aad8f12c1 <unknown>
#4 0x564aad92ca04 <unknown>
#5 0x564aad91203d <unknown>
#6 0x564aad92a369 <unknown>
#7 0x564aad911de3 <unknown>
#8 0x564aad8e5a7b <unknown>
#9 0x564aad8e681e <unknown>
#10 0x564aadb4b638 <unknown>
#11 0x564aadb4f507 <unknown>
#12 0x564aadb59c4c <unknown>
#13 0x564aadb50136 <unknown>
#14 0x564aadb1e9cf <unknown>
#15 0x564aadb73b98 <unknown>
#16 0x564aadb73d68 <unknown>
#17 0x564aadb82cb3 <unknown>
#18 0x7ff9b0a8c9eb <unknown>


### Operating System

arch linux

### Selenium version

4.8.2

### What are the browser(s) and version(s) where you see this issue?

chrome

### What are the browser driver(s) and version(s) where you see this issue?

ChromeDriver  116.0.5845.110 

### Are you using Selenium Grid?

_No response_
github-actions[bot] commented 1 year ago

@iyamk, thank you for creating this issue. We will troubleshoot it as soon as we can.


Info for maintainers

Triage this issue by using labels.

If information is missing, add a helpful comment and then I-issue-template label.

If the issue is a question, add the I-question label.

If the issue is valid but there is no time to troubleshoot it, consider adding the help wanted label.

If the issue requires changes or fixes from an external project (e.g., ChromeDriver, GeckoDriver, MSEdgeDriver, W3C), add the applicable G-* label, and it will provide the correct link and auto-close the issue.

After troubleshooting the issue, please add the R-awaiting answer label.

Thank you!

diemol commented 1 year ago

There is little we can do if the code works on non-headless and does not when it is headless.

Perhaps have a look at https://www.selenium.dev/blog/2023/headless-is-going-away/, https://developer.chrome.com/articles/new-headless/.

If you still face an issue, please report it to the Chrome/ChromeDriver folks. You can use the links below.

github-actions[bot] commented 1 year ago

Hi, @iyamk. This issue has been determined to require fixes in ChromeDriver.

You can see if the feature is passing in the Web Platform Tests.

If it is something new, please create an issue with the ChromeDriver team. Feel free to comment the issues that you raise back in this issue. Thank you.

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.