For automatic cleanup, use the context manager pattern:
import undetected as ucwith uc.Chrome() as driver: driver.get("https://example.com") print(driver.title)# Browser automatically closes when exiting the context
The context manager will reconnect the session on exit. If you want to fully quit the browser, call driver.quit() explicitly.
import undetected as ucdriver = uc.Chrome()driver.get("https://example.com")# Save screenshot to filedriver.save_screenshot("page.png")# Get screenshot as bytesscreenshot_bytes = driver.get_screenshot_as_png()driver.quit()
import undetected as ucfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as ECdriver = uc.Chrome()driver.get("https://example.com")# Wait up to 10 seconds for element to appearelement = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "dynamic-content")))driver.quit()
import undetected as ucfrom selenium.webdriver.common.by import Bydriver = uc.Chrome()driver.get("https://example.com")# Search all frames for elementsfor element in driver.find_elements_recursive(By.CLASS_NAME, "item"): print(element.text) # Element is usable immediately (frame context is already switched)driver.quit()
This generator function automatically switches between frames and yields elements in the correct context.
The library works best with default settings. Custom options, extensions, and startup arguments may trigger bot detection.From the source code (undetected/init.py:58-63):
Chrome has everything included to work out of the box. It does not need customizations. Any customizations MAY lead to trigger bot mitigation systems.
Use appropriate delays
Add natural delays between actions to mimic human behavior: