GShellAutomator/Automator/utils/browser_manager.py

114 lines
3.9 KiB
Python
Raw Normal View History

2021-08-23 22:01:40 +02:00
# 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
2021-09-01 17:37:03 +02:00
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
2021-08-23 22:01:40 +02:00
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
2021-08-23 22:01:40 +02:00
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
2021-08-23 22:01:40 +02:00
# 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
2021-08-23 22:01:40 +02:00
# 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
2021-08-23 22:01:40 +02:00
def start_browser(headless=False):
2021-08-23 22:01:40 +02:00
# # change mac
# # run_as_sudo('macchanger -r wlp3s0')
# time.sleep(3)
# #Set Chromedriver Options
opts = webdriver.ChromeOptions()
if headless is True:
opts.headless = True
2021-08-23 22:01:40 +02:00
opts.add_argument('--enable-javascript') #enabling javascript is needed in order to not get recognized as a bot
2021-09-01 17:37:03 +02:00
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)
2021-08-23 22:01:40 +02:00
2021-09-01 17:37:03 +02:00
print("[Browser_Manager] Starting new browser. Proxy: {} | UserAgent {}".format(PROXY, user_agent))
2021-08-23 22:01:40 +02:00
#Fire up chromedriver
chromedriver = webdriver.Chrome(executable_path='./chromedriver', options=opts)
2021-09-01 17:37:03 +02:00
2021-08-23 22:01:40 +02:00
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)
2021-09-01 17:37:03 +02:00
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()