0

I am using 2.33 chrome driver and trying to access variable defined in HTML as <script>var foo = "bar";</script> as follows:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
#chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
chrome_options.add_argument("--window-size=1920x1080")
chrome_options.add_argument("--enable-javascript")
driver_path = '<path>'
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=driver_path)
driver.get("<url>")
driver.execute_script("return window.foo")

and it works fine. However, if uncomment chrome_options.add_argument("--headless"), then, it returns None (and if I try to return $ it throws unknown error: $ is not defined).

Why using headless mode influences the results in a such a way?

Tzoiker
  • 1,354
  • 1
  • 14
  • 23
  • maybe this can help you to go around your problem: https://gist.github.com/anandsunderraman/e351485319a8a0e7df7e – Edwin Nov 21 '17 at 10:46
  • OMG, I found an error in my initial question. If "--headless" is enabled, then I get errors and strange behaviour. When it is not, it is OK, but I want to run headless. – Tzoiker Nov 21 '17 at 12:55
  • You should always make sure to set the user-agent as many sites end up blocking headless chrome, and it can be a real mind bender like what happened to me, carving up 2 days of my time. The solution was to set the user-agent. I set it like: options.add_arguments(f'user-agent={user_agent}') Where user_agent was dynamically set. See my answer here: https://stackoverflow.com/a/69464060/1871891 – user1871891 Oct 06 '21 at 11:26

0 Answers0