-1

My selenium script is not working on headless mode but working fine without headless. Error msg (NoSuchElementException)?why?

Increase time.sleep 2 to 10 but still not working

  • Share the code that you use for opening the browser and what is the chrome version you used – Zakaria Shahed Oct 31 '22 at 10:12
  • import time from selenium import webdriver from webdriver_manager.chrome import ChromeDriverManager from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service options = Options() options.headless = True ser = Service(ChromeDriverManager().install()) driver = webdriver.Chrome(service = ser, options=options) driver.maximize_window() – Praveen Yadav Oct 31 '22 at 10:48
  • def test_schema(): driver.get("https://search.google.com/test/rich-results") time.sleep(5) driver.find_element(By.XPATH,"//a[normalize-space()='Sign in']").click() time.sleep(5) time.sleep(10) driver.find_element(By.XPATH,"//span[normalize-space()='Next']").click() time.sleep(10) time.sleep(5) driver.find_element(By.XPATH,"//span[normalize-space()='Next']").click() time.sleep(5) – Praveen Yadav Oct 31 '22 at 10:52
  • I believe if the script executes fine in Non headless mode then it should definitely execute fine in headless mode also. probably there might be some other issue which was foreseen. try running the script multiple times in nonheadless mode you would easily catch the real culprit. – Rakesh Oct 31 '22 at 10:56

1 Answers1

0

Some of the applications do not work properly in the headless mode due to the application firewall. In that case, you need to debug what is showing inside headless.

Take a snapshot using selenium and see what is showing before the element not found exception

If you found your application is blocked to load in headless then follow the steps

To bypass the firewall you can use following argument in your python script. I used it in my java so i mention it in java. You can convert to your desire language

    options.addArguments("--headless");
    options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
    options.setExperimentalOption("useAutomationExtension", false);
    options.addArguments("user-agent=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36");
    
Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52