As March 2022 :
Hi, I had the same problem when using headless Selenium on a Docker Linux image.
I solved it by creating a virtualdisplay right before calling the webdriver:
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 800))
display.start()
Don't forget to install both pyvirtualdisplay and xvfb:
pip install pyvirtualdisplay and sudo apt-get install xvfb
And you must remove the "headless" option in ChromeDriver, here is the complete code I use :
#Display in order to avoid CloudFare bot detection
display = Display(visible=0, size=(800, 800))
display.start()
options = webdriver.ChromeOptions()
options.add_argument('--no-sandbox')
options.add_argument('start-maximized')
options.add_argument('enable-automation')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-browser-side-navigation')
options.add_argument("--remote-debugging-port=9222")
# options.add_argument("--headless")
options.add_argument('--disable-gpu')
options.add_argument("--log-level=3")
driver = webdriver.Chrome(ChromeDriverManager().install(), chrome_options=options)
Since it was working nicely without headless on my local computer, I figured emulate a real display might do the work aswell.
I do not really understand why, but from what I've understood, CloudFare tries to execute javascript code in order to confirm you're not a bot. Having a emulated webpage display helps to do so.