5

I have been playing around with Selenium + Chromedriver and I noticed I get different results based on if headless is enabled or disabled. After some investigation I found out that "headless" does not include the Accept-Language header.

Is there anyway to manually add this in the headers?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Arya
  • 8,473
  • 27
  • 105
  • 175
  • See if this helps? https://gist.github.com/c089/a5cbb834f9b54004de9b – Tarun Lalwani Oct 12 '17 at 21:58
  • right now I am setting the header like this, but it's not adding the header ` chromeOptions.put("args", Arrays.asList("headless", "window-size=1200x600", "--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", "--lang=en-US,en;q=0.8"));` – Arya Oct 12 '17 at 22:14
  • Open a bug with chromedriver then – Tarun Lalwani Oct 13 '17 at 05:04
  • Can you share with us the concerned URL? – undetected Selenium Oct 13 '17 at 07:02
  • @DebanjanB so there is another report here https://github.com/GoogleChrome/puppeteer/issues/677 and the website I was having this issue with is Facebook – Arya Oct 13 '17 at 18:23
  • 1
    at least now i know why the chromedriver works differently in headless mode. :D – gumuruh Jun 04 '18 at 23:49

2 Answers2

2

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/")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

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.

Michael Lihs
  • 7,460
  • 17
  • 52
  • 85