# from sudo import run_as_sudo import undetected_chromedriver as uc uc.install() from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import TimeoutException from selenium.webdriver.support import expected_conditions as EC import time import random import requests from random_user_agent.user_agent import UserAgent from random_user_agent.params import SoftwareName, OperatingSystem from utils.global_vars import PROXY def waitForElement(browser, by, selector, timeout=5, after_delay=0): try: return WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((by, selector))) except: return False def clickButton(browser, by, selector, timeout=7, after_delay=2): try: WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((by, selector))).click() time.sleep(after_delay) except: return False def inputText(browser, by, selector, text, timeout=5, interval=0.2, after_delay=2): try: element = WebDriverWait(browser, timeout).until(EC.element_to_be_clickable((by, selector))) for i in text: element.send_keys(i) time.sleep(interval) time.sleep(after_delay) except: return False def inputText_no_find(browser, element, text, timeout=5, interval=0.2, after_delay=2): try: for i in text: element.send_keys(i) time.sleep(interval) time.sleep(after_delay) except: return False # A lifesaver: https://stackoverflow.com/questions/36141681/does-anybody-know-how-to-identify-shadow-dom-web-elements-using-selenium-webdriv def select_shadow_element_by_css_selector(browser, selector): running_script = 'return document.querySelector("%s").shadowRoot' % selector element = browser.execute_script(running_script) return element # Another lifesaver: https://stackoverflow.com/questions/37384458/how-to-handle-elements-inside-shadow-dom-from-selenium def expand_shadow_element(driver, element): shadow_root = driver.execute_script('return arguments[0].shadowRoot', element) return shadow_root # This variant works on firefox https://stackoverflow.com/questions/67133483/accessing-shadowroot-via-selenium-in-firefox-returns-javascriptexception-cyclic # def expand_shadow_element(driver, element): # # return a list of elements # shadowRoot = driver.execute_script('return arguments[0].shadowRoot.children', element) # return shadowRoot def start_browser(headless=False): # # change mac # # run_as_sudo('macchanger -r wlp3s0') # time.sleep(3) # #Set Chromedriver Options opts = webdriver.ChromeOptions() if headless is True: opts.headless = True opts.add_argument('--enable-javascript') #enabling javascript is needed in order to not get recognized as a bot user_agent = random_user_agent() opts.add_argument("user-agent={}".format(user_agent)) if PROXY is not False: opts.add_argument(f'--proxy-server=socks5://'+PROXY) print("[Browser_Manager] Starting new browser. Proxy: {} | UserAgent {}".format(PROXY, user_agent)) #Fire up chromedriver chromedriver = webdriver.Chrome(executable_path='./chromedriver', options=opts) time.sleep(3) return chromedriver def quit_browser(driver): driver.delete_all_cookies() driver.quit() def delay(driver, waiting_time=5): driver.implicitly_wait(waiting_time) def random_user_agent(): software_names = [SoftwareName.CHROME.value] operating_systems = [OperatingSystem.WINDOWS.value, OperatingSystem.LINUX.value] user_agent_rotator = UserAgent(software_names=software_names, operating_systems=operating_systems, limit=100) #list of all user agents # user_agents = user_agent_rotator.get_user_agents() return user_agent_rotator.get_random_user_agent()