Selenium with chromedriver gives different results based on “headless” argument

☆樱花仙子☆ 提交于 2019-12-06 12:37:53
DrStrangelove

That's what Google chrome does. One way around it would be to use a proxy and modify the headers for you. Or you can use the Firefox driver as that driver does not send different headers when using the headless option.

Ideally, using and not using the --headless option shouldn't have any major effect on the elements within the DOM Tree getting rendered but may have a significant difference as far as the Viewport is concerned.

As an example, when ChromeDriver/Chrome is initialized along with the --headless option the default Viewport is

width = 800px, height = 600px 

Where as when ChromeDriver/Chrome is initialized without the --headless option the default Viewport is:

width = 1050px, height = 708px
  • Example Code (Python based):

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions()
    options.add_argument("--headless")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q")))
    print ("Headless Chrome Initialized")
    size = driver.get_window_size()
    print("Window size: width = {}px, height = {}px".format(size["width"], size["height"]))
    driver.quit()
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.google.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q")))
    print ("Chrome Initialized")
    size = driver.get_window_size()
    print("Window size: width = {}px, height = {}px".format(size["width"], size["height"]))
    
  • Console Output:

    Headless Chrome Initialized
    Window size: width = 800px, height = 600px
    Chrome Initialized
    Window size: width = 1050px, height = 708px
    

So it can be concluded that with --headless option ChromeDriver/Chrome opens the session with reduced Viewport and hence the number of elements identified are less.


Solution

While using ChromeDriver/Chrome to initiate a Browsing Instance always open in maximized mode:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("--headless")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://www.google.com/")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!