### Install undetected-chromedriver from GitHub Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Installs the undetected-chromedriver package directly from its GitHub repository. This is useful for installing the latest development version or a specific branch. Replace '@master' with '@branchname' if needed. ```bash pip install git+https://www.github.com/ultrafunkamsterdam/undetected-chromedriver@master ``` -------------------------------- ### Install undetected-chromedriver using pip Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Installs the undetected-chromedriver package using pip. This is the standard method for adding the library to your Python environment. Ensure you have pip installed and updated. ```bash pip install undetected-chromedriver ``` -------------------------------- ### Basic Undetected ChromeDriver Initialization Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Provides a straightforward example of initializing an undetected ChromeDriver instance and navigating to a website. This is the recommended method for general use and for users experiencing issues. It automatically handles necessary settings and browser executable detection. Requires the 'undetected_chromedriver' library. ```python import undetected_chromedriver as uc driver = uc.Chrome() driver.get( 'https://nowsecure.nl' ) # my own test test site with max anti-bot protection ``` -------------------------------- ### Navigate Website using Selenium WebDriver Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet demonstrates how to use Selenium WebDriver to open a specified URL in a browser. It requires the Selenium library to be installed. The input is a URL string, and the output is the browser navigating to that URL. ```python driver.get('https://nowsecure.nl') ``` -------------------------------- ### Use undetected-chromedriver as a Context Manager Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python example shows how to use undetected_chromedriver as a context manager. This approach simplifies resource management by automatically handling driver cleanup and session management upon exiting the `with` block. It's a convenient way to ensure the driver is properly closed after use. ```python import undetected_chromedriver as uc # Context manager automatically handles cleanup with uc.Chrome() as driver: driver.get('https://example.com') # Perform automation title = driver.title print(f"Page title: {title}") # Get page source source = driver.page_source # Execute JavaScript result = driver.execute_script(''' return { width: window.innerWidth, height: window.innerHeight, userAgent: navigator.userAgent }; ''') print(f"Browser info: {result}") # Driver is automatically quit on context exit # Manual enter/exit behavior: # __enter__: returns driver instance # __exit__: stops service, waits, restarts service, recreates session # This can be useful for session refresh scenarios ``` -------------------------------- ### Initialize Undetected ChromeDriver with Specific Version Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Shows how to initialize an undetected ChromeDriver instance while specifying a particular main version of Chrome to use. This is useful for resolving 'session not created' errors that occur when the ChromeDriver version is incompatible with the installed Chrome browser version. Requires the 'undetected_chromedriver' library. ```python import undetected_chromedriver as uc driver = uc.Chrome( version_main = 95 ) ``` -------------------------------- ### Network Request Headers Example (Python) Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet demonstrates typical network request headers captured during a web request. It includes details about the user agent, accepted content types, and security-related headers. This is often used to mimic legitimate browser traffic. ```python { 'method': 'Network.requestWillBeSentExtraInfo', 'params': { 'headers': { ':authority': 'nowsecure.nl', ':method': 'GET', ':path': '/cdn-cgi/images/trace/jschal/nojs/transparent.gif?ray=65444b779ae6546f', ':scheme': 'https', 'accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'en-US,en;q=0.9', 'referer': 'https://nowsecure.nl/', 'sec-ch-ua': '" Not A;Brand";v="99", 'Chromium';v="90", "Google 'Chrome";v="90"', 'sec-ch-ua-mobile': '?0', 'sec-fetch-dest': 'image', 'sec-fetch-mode': 'no-cors', 'sec-fetch-site': 'same-origin', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; 'x64) AppleWebKit/537.36 (KHTML, like 'Gecko) Chrome/90.0.4430.212 'Safari/537.36' }, 'requestId': '17180.4' } } ``` -------------------------------- ### Network Response Headers Example (Python) Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet shows typical network response headers received from a server. It includes caching directives, content information, and server details. This data is crucial for understanding how a server responds to requests and can be used for mimicking specific server behaviors. ```python { 'method': 'Network.responseReceivedExtraInfo', 'params': { 'headers': { 'accept-ranges': 'bytes', 'cache-control': 'max-age=7200\npublic', 'cf-ray': '65444b781d1fe604-LHR', 'content-length': '42', 'content-type': 'image/gif', 'date': 'Mon, 24 May 2021 05:58:53 GMT', 'etag': '"60a4d856-2a"', 'expires': 'Mon, 24 May 2021 07:58:53 GMT', 'last-modified': 'Wed, 19 May 2021 09:20:22 GMT', 'server': 'cloudflare', 'vary': 'Accept-Encoding', 'x-content-type-options': 'nosniff', 'x-frame-options': 'DENY' }, 'requestId': '17180.4', 'resourceIPAddressSpace': 'Public' } } ``` -------------------------------- ### Bypass DataDome with undetected-chromedriver Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This example compares a standard Selenium Chromedriver with an undetected-chromedriver instance when accessing DataDome. The undetected version uses headless mode to avoid detection. Requires undetected_chromedriver and Selenium. This is a powerful product, so using a clean IP is recommended. ```python # # STANDARD selenium Chromedriver # from selenium import webdriver chrome = webdriver.Chrome() chrome.get( 'https://datadome.co/customers-stories/toppreise-ends-web-scraping-and-content-theft-with-datadome/' ) chrome.save_screenshot( 'datadome_regular_webdriver.png' ) True # it caused my ip to be flagged, unfortunately # # UNDETECTED chromedriver (headless,even) # import undetected_chromedriver as uc options = uc.ChromeOptions() options.headless = True options.add_argument( '--headless' ) chrome = uc.Chrome( options = options ) chrome.get( 'https://datadome.co/customers-stories/toppreise-ends-web-scraping-and-content-theft-with-datadome/' ) chrome.save_screenshot( 'datadome_undetected_webddriver.png' ) ``` -------------------------------- ### Target Specific Chrome Version with undetected-chromedriver Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This example demonstrates how to target a specific version of Chrome when using undetected-chromedriver. It sets the TARGET_VERSION attribute before initializing the Chrome driver. This is useful for compatibility with specific browser versions. ```python import undetected_chromedriver as uc uc.TARGET_VERSION = 85 driver = uc.Chrome() ``` -------------------------------- ### Use Monkeypatch Mode with undetected-chromedriver in Python Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet illustrates how to use monkeypatch mode with undetected-chromedriver. It installs the necessary patches before importing the Selenium Chrome driver. This needs to be done before importing from the selenium package. ```python import undetected_chromedriver as uc uc.install() from selenium.webdriver import Chrome driver = Chrome() driver.get( 'https://distilnetworks.com' ) ``` -------------------------------- ### Listen to DevTools Protocol Events with Undetected-Chromedriver Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This example demonstrates how to enable and listen to low-level DevTools protocol events using undetected-chromedriver's expert mode. It sets up listeners for 'Network.dataReceived' and 'Network.requestWillBeSent' to capture and print network-related events. This feature is useful for in-depth network traffic analysis and debugging. ```python import undetected_chromedriver as uc from pprint import pformat driver = uc.Chrome(enable_cdp_events=True) def mylousyprintfunction(eventdata): print(pformat(eventdata)) # set the callback to Network.dataReceived to print (yeah not much original) driver.add_cdp_listener("Network.dataReceived", mylousyprintfunction) driver.get('https://nowsecure.nl') # known url using cloudflare's "under attack mode" def mylousyprintfunction(message): print(pformat(message)) # for more inspiration checkout the link below # https://chromedevtools.github.io/devtools-protocol/1-3/Network/ # and of couse 2 lousy examples driver.add_cdp_listener('Network.requestWillBeSent', mylousyprintfunction) driver.add_cdp_listener('Network.dataReceived', mylousyprintfunction) # hint: a wildcard captures all events! # driver.add_cdp_listener('*', mylousyprintfunction) ``` -------------------------------- ### Initialize Chrome with undetected-chromedriver in Python Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet shows the most basic way to initialize a Chrome driver instance using undetected-chromedriver. It imports the library and creates a Chrome driver, then navigates to a specified URL. This avoids basic bot detections. ```python import undetected_chromedriver as uc driver = uc.Chrome() driver.get( 'https://distilnetworks.com' ) ``` -------------------------------- ### Simplified Undetected ChromeDriver Import and Usage Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Demonstrates the simplified import and usage of undetected ChromeDriver after version 2 became the main module. Users can now import the library as 'uc' and initialize Chrome directly without needing to reference 'v2'. This change streamlines the process of setting up an undetected browser instance. Requires the 'undetected_chromedriver' library. ```python import undetected_chromedriver as uc driver = uc.Chrome() driver.get('https://nowsecure.nl') ``` -------------------------------- ### Initialize Undetected Chrome Browser Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt Initializes an undetected Chrome browser instance with automatic driver management and stealth configuration. It downloads the correct ChromeDriver, applies anti-detection patches, and configures optimal browser settings, creating a temporary user profile. ```python import undetected_chromedriver as uc # Basic initialization with automatic configuration driver = uc.Chrome() driver.get('https://nowsecure.nl') # The driver will automatically: # - Download the correct ChromeDriver version # - Apply anti-detection patches # - Configure optimal browser settings # - Create a temporary user profile # Perform automation search_box = driver.find_element('name', 'q') search_box.send_keys('undetected chromedriver') search_box.submit() # Always clean up driver.quit() ``` -------------------------------- ### Initialize and use undetected_chromedriver in Python Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Demonstrates how to initialize an undetected ChromeDriver instance in Python and navigate to a webpage. This code snippet showcases basic usage for web scraping or automation tasks where undetected browsing is required. It uses headless mode and specifies whether to use subprocess. ```python import undetected_chromedriver as uc driver = uc.Chrome(headless=True,use_subprocess=False) driver.get('https://nowsecure.nl') driver.save_screenshot('nowsecure.png') ``` -------------------------------- ### Manage ChromeDriver Binary with undetected-chromedriver Patcher Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python code demonstrates manual control over the ChromeDriver binary using the `Patcher` class from undetected_chromedriver. It allows specifying target Chrome versions, forcing patching, and managing multiprocessing settings. The snippet also shows how to use the patched driver and find the Chrome executable. ```python import undetected_chromedriver as uc from undetected_chromedriver import Patcher # Manual patcher usage patcher = Patcher( executable_path=None, # None = auto download force=False, # Force kill existing processes version_main=120, # Target Chrome version user_multi_procs=False # Enable for multiprocessing ) # Download and patch driver patcher.auto() print(f"Executable path: {patcher.executable_path}") print(f"Version: {patcher.version_main}.{patcher.version_full}") print(f"Is patched: {patcher.is_binary_patched()}") # Use patched driver options = uc.ChromeOptions() driver = uc.Chrome( options=options, driver_executable_path=patcher.executable_path, version_main=patcher.version_main, patcher_force_close=False, # Force close locked binaries user_multi_procs=False # Set True for parallel execution ) try: driver.get('https://example.com') # Find Chrome executable being used chrome_path = uc.find_chrome_executable() print(f"Chrome binary: {chrome_path}") except Exception as e: print(f"Error: {e}") finally: driver.quit() # Patcher cleanup # The patcher automatically cleans up on deletion # For manual cleanup: # patcher.cleanup_unused_files() ``` -------------------------------- ### Python: Configure ChromeOptions for undetected-chromedriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python script demonstrates how to configure `ChromeOptions` for `undetected-chromedriver`. It covers setting the user data directory, adding various browser arguments to control behavior (like disabling automation features), setting experimental preferences for downloads and notifications, and specifying the binary location if needed. ```python import undetected_chromedriver as uc # Create options object options = uc.ChromeOptions() # Set user data directory options.user_data_dir = "/tmp/chrome_profile" # Add browser arguments options.add_argument('--disable-blink-features=AutomationControlled') options.add_argument('--disable-dev-shm-usage') options.add_argument('--window-size=1920,1080') options.add_argument('--lang=en-US') # Set preferences (nested keys supported) options.add_experimental_option('prefs', { 'download.default_directory': '/tmp/downloads', 'download.prompt_for_download': False, 'profile.default_content_setting_values.notifications': 2, 'credentials_enable_service': False, 'profile.password_manager_enabled': False }) # Set binary location (if custom Chrome installation) # options.binary_location = '/path/to/chrome' # Initialize driver with options driver = uc.Chrome( options=options, driver_executable_path=None, # Auto-download if None browser_executable_path=None, # Auto-detect if None port=0, # Auto-assign port suppress_welcome=True, no_sandbox=True, log_level=0 ) try: driver.get('https://example.com') # Preferences will be applied to the profile except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` -------------------------------- ### Python: Automate Multiple Browser Windows with undetected-chromedriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python script demonstrates how to manage multiple browser windows and tabs using undetected-chromedriver. It covers opening new windows, switching between them, navigating to different URLs, and closing windows, providing a robust way to handle concurrent browser sessions. ```python import undetected_chromedriver as uc import time driver = uc.Chrome() sites = [ 'https://www.google.com', 'https://www.github.com', 'https://www.stackoverflow.com', 'https://www.python.org' ] try: # Open first site in main window driver.get(sites[0]) print(f"Main window: {driver.current_url}") # Create new windows for other sites for i in range(1, len(sites)): driver.window_new() time.sleep(0.5) print(f"Total windows: {len(driver.window_handles)}") # Switch to each window and navigate for idx, site in enumerate(sites[1:], start=1): driver.switch_to.window(driver.window_handles[idx]) driver.get(site) print(f"Window {idx}: {driver.current_url}") time.sleep(1) # Switch back to first window driver.switch_to.window(driver.window_handles[0]) print(f"Back to: {driver.current_url}") # Close all windows except last for handle in driver.window_handles[:-1]: driver.switch_to.window(handle) driver.close() # Attach to remaining window driver.switch_to.window(driver.window_handles[0]) except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` -------------------------------- ### Advanced Undetected ChromeDriver with Profile Folder Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Illustrates a more advanced usage of undetected ChromeDriver, including specifying a custom profile folder. If the folder doesn't exist, a new one is created. Data directories specified this way are not automatically removed on exit. Requires the 'undetected_chromedriver' library. ```python import undetected_chromedriver as uc options = uc.ChromeOptions() ``` -------------------------------- ### Customize Chrome with undetected-chromedriver in Python Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet demonstrates how to customize the Chrome driver with undetected-chromedriver. It sets a target Chrome version, specifies a custom chromedriver executable path, and adds a proxy argument. It navigates to a specified URL using the customized driver. ```python import undetected_chromedriver as uc # specify chromedriver version to download and patch uc.TARGET_VERSION = 78 # or specify your own chromedriver binary (why you would need this, i don't know) uc.install( executable_path = 'c:/users/user1/chromedriver.exe' , ) opts = uc.ChromeOptions() opts.add_argument( f'--proxy-server=socks5://127.0.0.1:9050' ) driver = uc.Chrome( options = opts ) driver.get( 'https://distilnetworks.com' ) ``` -------------------------------- ### Configure Chrome Profile and Version with Undetected-Chromedriver Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet shows how to set a custom user data directory for the Chrome profile and specify a particular Chrome version to use with undetected-chromedriver. This can be useful for managing browser state or ensuring compatibility with specific website behaviors. ```python # setting profile options.user_data_dir = "c:\\temp\\profile" # use specific (older) version driver = uc.Chrome( options = options , version_main = 94 ) # version_main allows to specify your chrome version instead of following chrome global version driver.get( 'https://nowsecure.nl' ) # my own test test site with max anti-bot protection ``` -------------------------------- ### Custom Profile and Version Management in Chrome Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt Configures the browser with a persistent user profile and specifies the Chrome version for compatibility. This allows for custom arguments and ensures the use of a particular Chrome major version, while also enabling options like running without a sandbox. ```python import undetected_chromedriver as uc # Initialize with options options = uc.ChromeOptions() # Set custom profile directory (persists between sessions) options.user_data_dir = "/path/to/profile" # Add custom arguments options.add_argument('--disable-blink-features=AutomationControlled') # Initialize with specific Chrome version driver = uc.Chrome( options=options, version_main=120, # Target specific major version headless=False, use_subprocess=True, no_sandbox=True ) try: driver.get('https://example.com') print(f"Current URL: {driver.current_url}") print(f"User agent: {driver.execute_script('return navigator.userAgent')}") finally: driver.quit() ``` -------------------------------- ### Direct Chrome DevTools Protocol (CDP) Control with undetected-chromedriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python code illustrates using undetected_chromedriver to directly control the browser via the Chrome DevTools Protocol (CDP). It enables advanced features like emulating devices, setting geolocation, overriding user agents, and injecting JavaScript. It also shows how to list and open new tabs using the CDP object. ```python import undetected_chromedriver as uc # Initialize with CDP enabled driver = uc.Chrome(enable_cdp_events=True) try: driver.get('https://example.com') # Execute CDP commands directly # Emulate device driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', { 'width': 375, 'height': 812, 'deviceScaleFactor': 3, 'mobile': True }) # Set geolocation driver.execute_cdp_cmd('Emulation.setGeolocationOverride', { 'latitude': 37.7749, 'longitude': -122.4194, 'accuracy': 100 }) # Override user agent user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X)' driver.execute_cdp_cmd('Network.setUserAgentOverride', { 'userAgent': user_agent }) # Inject JavaScript on every page load driver.execute_cdp_cmd('Page.addScriptToEvaluateOnNewDocument', { 'source': ''' Object.defineProperty(navigator, 'platform', { get: () => 'iPhone' }); ''' }) # Navigate with CDP modifications active driver.get('https://mobile-site.com') # Use CDP object for advanced operations from undetected_chromedriver.cdp import CDP cdp = CDP(driver.options) # List all tabs tabs = cdp.tab_list() for tab in tabs: print(f"Tab: {tab.title} - {tab.url}") # Open new tab with URL cdp.tab_new('https://github.com') except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` -------------------------------- ### Headless Mode with Enhanced Stealth Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt Runs the browser in headless mode with JavaScript patches specifically designed to hide headless detection. This ensures that even without a visible UI, the browser remains stealthy against bot detection mechanisms. ```python import undetected_chromedriver as uc # Headless mode with anti-detection options = uc.ChromeOptions() options.headless = True options.add_argument('--headless=new') driver = uc.Chrome( options=options, headless=True # Applies additional stealth patches ) try: driver.get('https://bot-detector-site.com') # Check if navigator.webdriver is properly hidden webdriver_detected = driver.execute_script('return navigator.webdriver') print(f"Webdriver detected: {webdriver_detected}") # Should be False # Take screenshot for verification driver.save_screenshot('headless_test.png') # Get page content page_source = driver.page_source except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` -------------------------------- ### Python: Advanced Element Finding with UCWebElement in undetected-chromedriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python script showcases the advanced element finding capabilities of undetected-chromedriver, specifically using the UCWebElement class. It demonstrates methods like `children()` for recursive and direct child element searching, accessing attributes via `.attrs`, and `find_elements_recursive()` for searching across frames. ```python import undetected_chromedriver as uc from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC driver = uc.Chrome(advanced_elements=True) try: driver.get('https://www.example.com') # Wait for element body = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, 'body')) ) # Use children() method (unique to UCWebElement) # Get direct children for child in body.children(): print(f"Tag: {child.tag_name}") # Get recursive children of specific tag all_links = body.children('a', recursive=True) for link in all_links: href = link.get_attribute('href') print(f"Link: {href}") # Get images recursively all_images = body.children('img', recursive=True) for img in all_images: src = img.attrs.get('src') # attrs is UCWebElement feature print(f"Image src: {src}") # Find elements across all frames for elem in driver.find_elements_recursive(By.CSS_SELECTOR, 'input[type="text"]'): print(f"Found input in frame: {elem.get_attribute('name')}") # Safe click (less detectable) # button.click_safe() # Alternative to regular click() except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` -------------------------------- ### Find Child Elements with WebElement.children() Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md Demonstrates how to use the WebElement.children() method to find child nodes within a given element. It shows how to retrieve a specific child by index and then find all 'img' tags within that child recursively. This method helps in easily navigating and extracting data from the DOM structure. It requires the 'undetected_chromedriver' library. ```python body = driver.find_element('tag name', 'body') # get the 6th child (any tag) of body, and grab all img's within (recursive). images = body.children()[6].children('img', True) srcs = list(map(lambda _:_.attrs.get('src'), images)) ``` -------------------------------- ### Log Network Request Events with Chrome DevTools Protocol Source: https://github.com/ultrafunkamsterdam/undetected-chromedriver/blob/master/README.md This snippet illustrates how to log network request events using the Chrome DevTools Protocol. It captures details about requests, including URL, headers, and timing. This is useful for network analysis during automated browser sessions. ```json { "method": "Network.requestWillBeSent", "params": { "documentURL": "https://nowsecure.nl/", "frameId": "F42BAE4BDD4E428EE2503CB5A7B4F700", "hasUserGesture": false, "initiator": {"type": "other"}, "loaderId": "449906A5C736D819123288133F2797E6", "request": { "headers": { "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36", "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"", "sec-ch-ua-mobile": "?0" }, "initialPriority": "VeryHigh", "method": "GET", "mixedContentType": "none", "referrerPolicy": "strict-origin-when-cross-origin", "url": "https://nowsecure.nl/" }, "requestId": "449906A5C736D819123288133F2797E6", "timestamp": 190010.996717, "type": "Document", "wallTime": 1621835932.112026 } } ``` ```json { "method": "Network.requestWillBeSentExtraInfo", "params": { "associatedCookies": [], "headers": { ":authority": "nowsecure.nl", ":method": "GET", ":path": "/", ":scheme": "https", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "accept-encoding": "gzip, deflate, br", "accept-language": "en-US,en;q=0.9", "sec-ch-ua": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"90\", \"Google Chrome\";v=\"90\"", "sec-ch-ua-mobile": "?0", "sec-fetch-dest": "document", "sec-fetch-mode": "navigate", "sec-fetch-site": "none", "sec-fetch-user": "?1", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36" }, "requestId": "449906A5C736D819123288133F2797E6" } } ``` ```json { "method": "Network.responseReceivedExtraInfo", "params": { "blockedCookies": [], "headers": { "alt-svc": "h3-27=\":443\"; ma=86400, h3-28=\":443\"; ma=86400, h3-29=\":443\"; ma=86400", "cache-control": "private, max-age=0, no-store, no-cache, must-revalidate, post-check=0, pre-check=0", "cf-ray": "65444b779ae6546f-LHR", "cf-request-id": "0a3e8d7eba0000546ffd3fa000000001", "content-type": "text/html; charset=UTF-8", "date": "Mon, 24 May 2021 05:58:53 GMT", "expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"", "expires": "Thu, 01 Jan 1970 00:00:01 GMT", "nel": "{\"report_to\": \"cf-nel\", \"max_age\": 604800}", "permissions-policy": "accelerometer=(),autoplay=(),camera=(),clipboard-read=(),clipboard-write=(),fullscreen=(),geolocation=(),gyroscope=(),hid=(),interest-cohort=(),magnetometer=(),microphone=(),payment=(),publickey-credentials-get=(),screen-wake-lock=(),serial=(),sync-xhr=(),usb=()", "report-to": "{\"endpoints\":[{\"url\": \"https:\/\/a.nel.cloudflare.com\/report?s=CAfobYlmWImQ90e%2B4BFBhpPYL%2FyGyBvkcWAj%2B%2FVOLoEq0NVrD5jU9m5pi%2BKI%2BOAnINLPXOCoX2psLphA5Z38aZzWNr3eW%2BDTIK%2FQidc%3D\"}],\"group\": \"cf-nel\", \"max_age\": 604800}", "server": "cloudflare", "vary": "Accept-Encoding", "x-frame-options": "SAMEORIGIN" }, "requestId": "449906A5C736D819123288133F2797E6", "resourceIPAddressSpace": "Public" } } ``` -------------------------------- ### CDP Event Listening with Undetected ChromeDriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt Subscribes to Chrome DevTools Protocol (CDP) events for advanced network monitoring and debugging. This allows for real-time capture and processing of network-related events, such as data received and requests being sent. ```python import undetected_chromedriver as uc from pprint import pformat # Enable CDP events driver = uc.Chrome(enable_cdp_events=True) # Define callback for network events def network_data_callback(event_data): print(f"Network Data Received:") print(pformat(event_data)) def request_callback(event_data): request_id = event_data.get('params', {}).get('requestId') url = event_data.get('params', {}).get('request', {}).get('url') print(f"Request {request_id}: {url}") # Add CDP listeners driver.add_cdp_listener('Network.dataReceived', network_data_callback) driver.add_cdp_listener('Network.requestWillBeSent', request_callback) # Use wildcard to capture all events # driver.add_cdp_listener('*', lambda data: print(pformat(data))) try: driver.get('https://nowsecure.nl') # All network events will be printed via callbacks finally: # Clear listeners if needed driver.clear_cdp_listeners() driver.quit() ``` -------------------------------- ### Reconnect Chrome Session with undetected-chromedriver Source: https://context7.com/ultrafunkamsterdam/undetected-chromedriver/llms.txt This Python snippet demonstrates how to reconnect to a Chrome session using undetected_chromedriver. It's useful for bypassing detection mechanisms or recovering a session. The `reconnect` method stops and restarts the chromedriver service, creating a new session. ```python import undetected_chromedriver as uc import time driver = uc.Chrome() try: driver.get('https://example.com') # Perform initial actions print(f"Initial session: {driver.session_id}") # If heavy detection is encountered, try reconnecting # This stops and restarts the chromedriver service # and recreates the session # Simulate need for reconnection time.sleep(2) print("Reconnecting to bypass detection...") driver.reconnect(timeout=0.5) print(f"New session: {driver.session_id}") # Continue automation with new session driver.get('https://another-site.com') # Manual session management # driver.start_session() # Create new session with current capabilities except Exception as e: print(f"Error: {e}") finally: driver.quit() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.