markmelnic / stealthenium

Run selenium undetected.
MIT License
10 stars 0 forks source link

Doesnt work with Remote driver #2

Open danilivanyuk opened 1 day ago

danilivanyuk commented 1 day ago
  def create_browser_session(user_id: int = 1):
    options = webdriver.ChromeOptions()
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option("useAutomationExtension", False)

    script_dir = os.path.dirname(os.path.abspath(__file__))
    base_directory = os.path.join(script_dir, "users")
    user_directory = os.path.join(base_directory, f"user_{user_id}")
    options.add_argument("--disable-dev-shm-usage")
    options.add_argument("--disable-notifications")
    options.add_argument("--disable-popup-blocking")
    options.add_argument("--no-sandbox")

    ROOT_DIR = f"{ Path(__file__).resolve().parents[0] }/meeting_recording"
    options.add_experimental_option(
        "prefs",
        {
            "download.default_directory": ROOT_DIR,
            "download.prompt_for_download": False,
            "download.directory_upgrade": True,
            "safebrowsing.enabled": True,
        },
    )
    remote_connection = ClientConfig(remote_server_addr="http://localhost:4444/wd/hub")
    driver = webdriver.Remote(
        options=options,
        client_config=remote_connection,
    )
    ua = get_random_chrome_user_agent()
    stealth(
        driver=driver,
        user_agent=ua,
        languages=["ru-RU", "ru"],
        vendor="Google Inc.",
        platform="Win32",
        webgl_vendor="Intel Inc.",
        renderer="Intel Iris OpenGL Engine",
        fix_hairline=True,
        run_on_insecure_origins=True,
    )

    execute_cdp_cmd(
        driver=driver,
        cmd="Page.addScriptToEvaluateOnNewDocument",
        params={
            "source": """
            delete window.cdc_adoQpoasnfa76pfcZLmcfl_Array;
            delete window.cdc_adoQpoasnfa76pfcZLmcfl_Promise;
            delete window.cdc_adoQpoasnfa76pfcZLmcfl_Symbol;
      """
        },
    )
    sleep(10)
    return driver

create_browser_session()

Hi, when creating Remote Driver and calling stealth in result I get error with _url property File "/home/lib64/python3.11/site-packages/stealthenium/cdp.py", line 14, in execute_cdp_cmd url =driver.command_executor._url + resource ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'ChromeRemoteConnection' object has no attribute '_url'

if I change driver.command_executor._url in cdp.py to http://localhost:4444/wd/hub its working

markmelnic commented 3 hours ago

The issue here is that command_executor is being set upon initializing webdriver.Remote. It is not being configured when using ClientConfig. Quick fix:

driver = webdriver.Remote(
    command_executor="http://localhost:4444/wd/hub"
    options=options,
    client_config=remote_connection,
)

Haven't tested this, so let me know what are the results.