Функция open_chrome
для качественного открытия Selenium chrome
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from fake_useragent import UserAgent
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec
def open_chrome(ignore_images=True, headless=True, using_user_data=False, proxy=None, fake_user_agent=True, eager_loading=True, chromedriver_path=None):
global driver
try:
driver
except:
driver = None
if driver != None:
driver.quit()
options = Options()
options.add_argument('--dns-prefetch-disable')
options.add_argument('--disable-extensions')
options.add_argument('--disable-gpu')
options.add_argument('--disable-browser-side-navigation')
options.add_argument('--disable-infobars')
options.add_argument('--disable-dev-shm-usage')
options.add_argument("--start-maximized")
options.add_argument("--disable-extensions")
prefs = {
'profile.managed_default_content_setting_values': {
'cookies': 1,
'images': 2,
'javascript': 2,
'plugins': 2,
'popups': 2,
'geolocation': 2,
'notifications': 2,
'auto_select_certificate': 2,
'fullscreen': 2,
'mixed_script': 2,
'media_stream': 2,
'media_stream_mic': 2,
'media_stream_camera': 2,
'protocol_handlers': 2,
'push_messaging': 2,
'ppapi_broker': 2,
'automatic_downloads': 2,
'midi_sysex': 2,
'ssl_cert_decisions': 2,
'metro_switch_to_desktop': 2,
'protected_media_identifier': 2,
'app_banner': 2,
'site_engagement': 2,
'durable_storage': 2
},
"profile.managed_default_content_settings.images": 2
}
options.add_experimental_option("prefs", prefs)
if proxy:
options.add_argument(f'--proxy-server={proxy}')
if using_user_data:
options.add_argument("user-data-dir=/Users/lotfull/Library/Application Support/Google/Chrome/")
else:
options.add_argument('--no-sandbox')
options.add_argument("user-data-dir=/tmp/chrome_run")
if headless:
options.add_argument('--headless')
if fake_user_agent:
user_agent = UserAgent().chrome
options.add_argument(f"user-agent={user_agent}")
capabilities = DesiredCapabilities.CHROME
if eager_loading:
capabilities["pageLoadStrategy"] = "eager"
if chromedriver_path is None:
if os.path.exists('/Users/lotfull/chromedriver'):
chromedriver_path = '/Users/lotfull/chromedriver'
driver = webdriver.Chrome(chromedriver_path,
desired_capabilities=capabilities,
options=options)
return driver
def screen():
from IPython.display import Image, display
driver.save_screenshot('screen.png')
display(Image('screen.png'))