Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Getting '403 Forbidden' error when using headless chrome with python selenium

When using regualr chromedriver, my tests run without any issues and I get the test results. When I am trying to launch headless chrome with python and selenium, I get a ‘403 Forbidden’ error on the screenshot and selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: on the console. I passed all these arguments and still get the same result :

chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--window-size=1920,1080')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--allow-running-insecure-content')
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(chrome_options=chrome_options)

I also tried the from fake_headers import Headers but the issue still persists

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

#!/usr/bin/python3
#try this 
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium import webdriver
import time,os
from bs4 import BeautifulSoup
from pyvirtualdisplay import Display
from fake_useragent import UserAgent
def web(url):
    display = Display(visible=0, size=(1920, 1080)) 
    display.start()
    ua = UserAgent()
    userAgent = ua.chrome
    chrome_options = Options()
    chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
    chrome_options.add_experimental_option('useAutomationExtension', False)
    chrome_options.add_argument("--disable-blink-features=AutomationControlled")
    chrome_options.add_argument(f'user-agent={userAgent}')
    driver = webdriver.Chrome (options = chrome_options)
    driver.get(url)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading