0

I had an selenium application that worked perfectly until a few days, I don't know what happened but for some reason it stopped and debugging a little I noticed that when I remove the headless it works normally, but I wanted to keep it in the code to make it invisible , help me please.

from selenium.webdriver.chrome.service import Service; 
import undetected_chromedriver as uc;
import time;
import os; 

chrome_options = uc.ChromeOptions();
chrome_options.add_argument("--disable-dev-shm-usage"); 
chrome_options.add_argument("--window-size=1920,1080"); 
chrome_options.add_argument("--disable-automation"); 
chrome_options.add_argument("--disable-extensions"); 
chrome_options.add_argument("--disable-gpu"); 
chrome_options.add_argument("--no-sandbox"); 
chrome_options.add_argument("--headless"); 
chrome_options.add_argument("--kiosk"); 

chrome_options.binary_location = os.environ.get("GOOGLE_CHROME_BIN");
service = Service(executable_path=os.environ.get("CHROMEDRIVER_PATH"));
driver = uc.Chrome(options=chrome_options, service=service);
driver.get('https://www.google.com/');

print(driver.page_source);
Michael Mintz
  • 9,007
  • 6
  • 31
  • 48
  • 2
    what issue do you face? error trace please – Yuri R Jul 20 '23 at 22:37
  • There are no clear errors because apparently everything "works" in my case it's an automation that performs a simple task, when I run it without chrome_options.add_argument("--headless"); the automation works, when I put it, the automation doesn't work and curiously no error is generated. – Rodrigo Carvalho Jul 21 '23 at 01:25

2 Answers2

0

You may try using webdriver-manager and the headless mode works as expected.

import undetected_chromedriver as uc
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager

chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-dev-shm-usage")
chrome_options.add_argument("--window-size=1920,1080")
chrome_options.add_argument("--disable-automation")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--kiosk")


driver = uc.Chrome(service=ChromeService(ChromeDriverManager().install()), options=chrome_options)
driver.get('https://www.google.com/')

print(driver.page_source)

output:

<html itemscope="" itemtype="http://schema.org/WebPage" lang="vi">
 <head>
  <meta charset="utf-8"/>
  <meta content="origin" name="referrer"/>
  <meta content="/images/branding/googleg/1x/googleg_standard_color_128dp.png" itemprop="image"/>
  <title>
   Google
  </title>
  <script async="" nonce="" src="https://apis.google.com/_/scs/abc-static/_/js/k=gapi.gapi.en.hh2Jqle7bK0.O/m=gapi_iframes,googleapis_client/rt=j/sv=1/d=1/ed=1/rs=AHpOoo-jeiq7uVLkyqJvSohFtUkaGjEuyg/cb=gapi.loaded_0">
  .
  .
  .
  .
  </script>
  <script async="" nonce="" src="/xjs/_/js/k=xjs.s.vi.NakJ3SguYrQ.O/ck=xjs.s.Gs4Oqa-rJMo.L.W.O/am=CAAAAAAAAAIARAPhEMAGECAAAAIAACBAAAAAAAAcRAAwACB4lEkCAAAChJAwEMRgAwASSgAAAAAAYX9EAAAAAGAAAgAAQiEAGIiACiAAAAAA8gAEPABgMGEBAAAAAAAAAABAACUIBhckAAoCQAAAAAAAAAAAACCVTF4cCAE/d=0/dg=2/br=1/rs=ACT90oE-CFNb8-iy9HhYm8GVrVv57Ud-IQ/m=sy1t,sydv,sydy,WlNQGd,syoz,nabPbb,ANyn1,sydw,CnSW2d,kQvlef,syjj,fXO0xe,syjh,U4MzKc,g8nkx,sy9o,syjk,syjl,syjm,syjn,DPreE?xjs=s3">
  </script>
  <script async="" gapi_processed="true" nonce="" src="/xjs/_/js/k=xjs.s.vi.NakJ3SguYrQ.O/ck=xjs.s.Gs4Oqa-rJMo.L.W.O/am=CAAAAAAAAAIARAPhEMAGECAAAAIAACBAAAAAAAAcRAAwACB4lEkCAAAChJAwEMRgAwASSgAAAAAAYX9EAAAAAGAAAgAAQiEAGIiACiAAAAAA8gAEPABgMGEBAAAAAAAAAABAACUIBhckAAoCQAAAAAAAAAAAACCVTF4cCAE/d=0/dg=2/br=1/rs=ACT90oE-CFNb8-iy9HhYm8GVrVv57Ud-IQ/m=sy7f,sy7g,aLUfP?xjs=s3">
  </script>
 </body>
</html>

You'll need to install webdriver-manager:

pip install webdriver-manager
Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
  • Sorry, but that doesn't work either, see the code I posted, it brings the same answer as yours. To better understand pretend that what I want to run is driver.execute_script(f'document.elementFromPoint({999}, {999}).click();') what I mean is that this only runs with headless mode off, and my intention is to turn it on. – Rodrigo Carvalho Jul 21 '23 at 02:34
  • it's working for me in both modes headless off and headless on – Ajeet Verma Jul 21 '23 at 06:06
  • Hello, I reposted this question in: https://stackoverflow.com/questions/76734991/selenium-automation-not-working-when-active-in-headless-mode/76735163#76735163 maybe it better explains my intentions – Rodrigo Carvalho Jul 21 '23 at 12:26
0

Try the newer Chrome headless mode, which gets the same results as non-headless mode: https://stackoverflow.com/a/73840130/7058266

chrome_options.add_argument("--headless=new")

If something works in regular Chrome, it should now work with the newer headless mode too.

Michael Mintz
  • 9,007
  • 6
  • 31
  • 48