Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ahmedshahriar/471b4df7dabc6a422c97ad663027e018 to your computer and use it in GitHub Desktop.
Save ahmedshahriar/471b4df7dabc6a422c97ad663027e018 to your computer and use it in GitHub Desktop.
"""
code snippet for selenium with fake user agent
selenium : https://selenium-python.readthedocs.io/
fake user agent : https://github.com/hellysmile/fake-useragent
https://github.com/ahmedshahriar
"""
from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from fake_useragent import UserAgent
# user agent setup
ua = UserAgent()
a = ua.random
user_agent = ua.random
print(user_agent)
CHROME_DRIVER_PATH = "C:\\Users\\...\\chromedriver.exe"
chrome_options = Options()
chrome_options.binary_location = "C:\Program Files\Google\Chrome\Application\chrome.exe" # for windows
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument(f'user-agent={user_agent}')
webdriver = webdriver.Chrome(
executable_path=CHROME_DRIVER_PATH,
options=chrome_options,
)
given_url = "https://gist.github.com/"
with webdriver as driver:
# timeout
wait = WebDriverWait(driver, 10)
driver.get(url=given_url)
sleep(0.5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment