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
CodePudding user response:
#!/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)