webdriver_template/main.py

45 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-08-10 14:48:21 +03:00
import asyncio
from time import sleep
from typing import Callable, List
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver import Chrome
from fake_useragent import UserAgent
def openBrowser( proxy:str|None, ) -> webdriver.Chrome:
print(f"--proxy-server={proxy}");
agent = UserAgent().random
options = Options()
options.add_argument('--no-sandbox')
options.add_argument("--mute-audio")
if proxy is not None:
options.add_argument(f"--proxy-server={proxy}")
options.add_argument(f"user-agent={agent}")
options.add_argument('--disable-gpu')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--start-maximized')
# options.add_argument('--headless')
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
# driver = webdriver.Firefox(options= foptions)
driver = webdriver.Chrome(options=options)
driver.execute_cdp_cmd("Page.addScriptToEvaluateOnNewDocument", {
'source': '''
for (let prop in window) {
if (prop.startsWith('cdc_')) {
delete window[prop];
}
}
'''})
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.set_page_load_timeout(100) # Таймаут загрузки страницы
driver.implicitly_wait(10) # Неявное ожидание
# sleep(10)
return driver;