ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
9.58k stars 1.14k forks source link

It did not work in headless mode #1149

Open freemind002 opened 1 year ago

freemind002 commented 1 year ago

Here is my code

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import undetected_chromedriver as uc
from fake_useragent import UserAgent

class Demo:
    def set_chrome_option(self):
        chrome_options = uc.ChromeOptions()
        # chrome_options.headless = True
        chrome_options.add_experimental_option('prefs', {'profile.default_content_setting_values': {'notifications': 2}})
        chrome_options.add_argument('disable-infobars')
        chrome_options.add_argument("--window-size=1920,1080")
        # chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        chrome_options.add_argument(f"user-agent={UserAgent().random}")

        return chrome_options

    def run_all(self):
        browser = uc.Chrome(options=self.set_chrome_option())
        browser.get("https://pypi.org/project/undetected-chromedriver/")
        browser.implicitly_wait(200)
        element = browser.find_element(By.XPATH, '//*[@id="content"]/div[3]/div/div[1]/h1').text
        print(element)
        browser.quit()

    def main(self):
        self.run_all()

if __name__ == "__main__":
    demo = Demo()
    demo.main()

It did work but when I try this way

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium import webdriver
import undetected_chromedriver as uc
from fake_useragent import UserAgent

class Demo:
    def set_chrome_option(self):
        chrome_options = uc.ChromeOptions()
        chrome_options.headless = True
        chrome_options.add_experimental_option('prefs', {'profile.default_content_setting_values': {'notifications': 2}})
        chrome_options.add_argument('disable-infobars')
        chrome_options.add_argument("--window-size=1920,1080")
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('--no-sandbox')
        chrome_options.add_argument('--disable-dev-shm-usage')
        chrome_options.add_argument('blink-settings=imagesEnabled=false')
        chrome_options.add_argument(f"user-agent={UserAgent().random}")

        return chrome_options

    def run_all(self):
        browser = uc.Chrome(options=self.set_chrome_option())
        browser.get("https://pypi.org/project/undetected-chromedriver/")
        browser.implicitly_wait(200)
        element = browser.find_element(By.XPATH, '//*[@id="content"]/div[3]/div/div[1]/h1').text
        print(element)
        browser.quit()

    def main(self):
        self.run_all()

if __name__ == "__main__":
    demo = Demo()
    demo.main()

it did not work, it crashed and said:

Traceback (most recent call last):
  File ".\demo.py", line 39, in <module>
    demo.main()
  File ".\demo.py", line 34, in main
    self.run_all()
  File ".\demo.py", line 26, in run_all
    browser = uc.Chrome(options=self.set_chrome_option())
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\undetected_chromedriver\__init__.py", line 441, in __init__
    super(Chrome, self).__init__(
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 69, in __init__
    super().__init__(DesiredCapabilities.CHROME['browserName'], "goog",
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\chromium\webdriver.py", line 92, in __init__
    super().__init__(
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 270, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\undetected_chromedriver\__init__.py", line 704, in start_session
    super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 363, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in execute
    self.error_handler.check_response(response)
  File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 243, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:12856
from chrome not reachable
Stacktrace:
Backtrace:
        (No symbol) [0x010FDCE3]
        (No symbol) [0x010939D1]
        (No symbol) [0x00FA4C70]
        (No symbol) [0x00F9AFA5]
        (No symbol) [0x00FC4A28]
        (No symbol) [0x00FBEE51]
        (No symbol) [0x00FBEC31]
        (No symbol) [0x00FF15AE]
        (No symbol) [0x00FF129C]
        (No symbol) [0x00FEACC6]
        (No symbol) [0x00FC6F68]
        (No symbol) [0x00FC80CD]
        GetHandleVerifier [0x01373832+2506274]
        GetHandleVerifier [0x013A9794+2727300]
        GetHandleVerifier [0x013AE36C+2746716]
        GetHandleVerifier [0x011A6690+617600]
        (No symbol) [0x0109C712]
        (No symbol) [0x010A1FF8]
        (No symbol) [0x010A20DB]
        (No symbol) [0x010AC63B]
        BaseThreadInitThunk [0x76EB00F9+25]
        RtlGetAppContainerNamedObjectPath [0x77D47BBE+286]
        RtlGetAppContainerNamedObjectPath [0x77D47B8E+238]

how can I fix this problem?

pranavpa8788 commented 1 year ago

https://stackoverflow.com/questions/69407211/cannot-connect-to-chrome-at-127-0-0-137541-when-using-undetected-chromedriver First result from google search of the error, give it a shot

soft-top-notch commented 1 year ago
  1. Please check your uc version if it's up-to-date first.
  2. If it's good, then, please try this: driver = uc.Chrome(headless=True)
  3. If it still doesn't work, please use pyvirtualdisplay to run the virtualdisaply and then run your chrome.

Note: According to my experience, headless mode is detected somehow in some websites ( although non-headless mode is not detected. ). So I don't use headless. :)

OpsecGuy commented 1 year ago

Thats a fix for other people having problems with running in Headless mode

Set chromedriver version if calling headless=True

uc.Chrome(driver_executable_path=self.webdriver_path(), options=arg, headless=True, version_main=112)

anilabhadatta commented 1 year ago

@OpsecGuy your solution is working but is it possible to remove version_main from parameters since it has to be given manually after updating chromedriver