Skip to content

Instantly share code, notes, and snippets.

@evan-kolberg
Last active February 1, 2023 23:12
Show Gist options
  • Save evan-kolberg/e655f97bb9e50675f1ea9cf87947e2bd to your computer and use it in GitHub Desktop.
Save evan-kolberg/e655f97bb9e50675f1ea9cf87947e2bd to your computer and use it in GitHub Desktop.
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from random_user_agent.user_agent import UserAgent
from random_user_agent.params import SoftwareName, OperatingSystem
from selenium_stealth import stealth
software_names = [SoftwareName.CHROME.value, SoftwareName.FIREFOX.value, SoftwareName.SAFARI.value]
operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value, OperatingSystem.MACOS.value]
user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100)
user_agent = user_agent_rotator.get_random_user_agent()
print(f'User Agent: {user_agent}')
options = webdriver.ChromeOptions()
options.add_experimental_option('excludeSwitches', ['enable-automation'])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
options.add_argument(f'--user-agent={user_agent}')
options.add_argument('--window-size=960,540')
options.add_argument('--incognito')
# options.add_experimental_option('detach', True) # prevents driver to close automatically, but detaches
options.binary_location = "/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"
driver = webdriver.Chrome(service=Service('/chromedriver'), options=options)
stealth(driver, languages=['en-US', 'en'], vendor='Google Inc.', platform='Win32', webgl_vendor='Intel Inc.', renderer='Intel Iris OpenGL Engine', fix_hairline=True)
driver.set_window_position(0, 0, windowHandle='current')
driver.get('https://google.com')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment