Last active
December 13, 2021 15:16
-
-
Save im-noob/c29e154d2fac77163cc3ed74172f3702 to your computer and use it in GitHub Desktop.
Selenium Starter 3 Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !apt-get update | |
# !apt install chromium-chromedriver | |
# !cp /usr/lib/chromium-browser/chromedriver /content/drive/MyDrive/Colab\ Notebooks | |
# !pip install selenium | |
# !chmod 0777 chromedriver | |
import json | |
import os | |
from time import sleep | |
from selenium import webdriver | |
from selenium.webdriver.chrome.options import Options | |
from selenium.webdriver.common.keys import Keys | |
from selenium.webdriver.support import expected_conditions as EC | |
from selenium.webdriver.support.ui import WebDriverWait | |
from selenium.common.exceptions import TimeoutException | |
from selenium.webdriver.common.by import By | |
from selenium.common.exceptions import StaleElementReferenceException | |
from selenium.common.exceptions import NoSuchElementException | |
print('Starting...') | |
url = "https://youtube.com/" | |
# CHROME_PATH = '/usr/bin/google-chrome' | |
CHROMEDRIVER_PATH = '/content/drive/MyDrive/Colab Notebooks/chromedriver' | |
WINDOW_SIZE = "1920,1080" | |
chrome_options = Options() | |
chrome_options.headless = True | |
chrome_options.add_argument("--headless") | |
# chrome_options.add_argument("--window-size=%s" % WINDOW_SIZE) | |
# chrome_options.add_argument('--user-data-dir=./User_Data') | |
chrome_options.add_argument('--disable-blink-features=AutomationControlled') | |
chrome_options.add_experimental_option("useAutomationExtension", False) | |
chrome_options.add_experimental_option("excludeSwitches",["enable-automation"]) | |
chrome_options.add_argument("--no-sandbox") | |
chrome_options.add_argument("--disable-dev-shm-usage") | |
# chrome_options.add_argument("--disable-gpu") | |
# chrome_options.add_argument("--remote-debugging-port=9222") | |
# chrome_options.add_experimental_option("excludeSwitches",["ignore-certificate-errors"]) | |
print('initiated...') | |
preferences = { | |
# "profile.default_content_settings.popups": 0, | |
"download.default_directory": os.getcwd() + os.path.sep, | |
# "directory_upgrade": True | |
} | |
chrome_options.add_experimental_option('prefs', preferences) | |
# chrome_options.binary_location = CHROME_PATH | |
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, options=chrome_options) | |
driver.maximize_window() | |
driver.get(url) | |
# Comman function for init driver | |
def init_driver(url): | |
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH) | |
driver.maximize_window() | |
driver.get(url) | |
return driver | |
## https://stackoverflow.com/questions/53039551/selenium-webdriver-modifying-navigator-webdriver-flag-to-prevent-selenium-detec | |
def init_driver(url): | |
chrome_options = webdriver.ChromeOptions() | |
chrome_options.add_argument('--disable-blink-features=AutomationControlled') | |
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"]) | |
chrome_options.add_experimental_option('useAutomationExtension', False) | |
driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH,options=chrome_options) | |
driver.maximize_window() | |
driver.get(url) | |
return driver | |
def wait_for_page_load(xpath_url): | |
try: | |
element_present = EC.presence_of_element_located((By.XPATH, xpath_url)) | |
WebDriverWait(driver, 5).until(element_present) | |
except TimeoutException: | |
print("Timed out waiting for page to load") | |
except NoSuchElementException: | |
print("Element not found") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment