from selenium import * from selenium import webdriver, common import os import sys from pyOpenRPA.Tools import CrossOS import time UIO_WAIT_SEC_FLOAT = 60 UIO_WAIT_INTERVAL_SEC_FLOAT = 1.0 gBrowser:webdriver.Chrome = None def BrowserChromeStart(inDriverExePathStr:str = None, inChromeExePathStr:str = None, inExtensionPathList:list = None, inProfilePathStr:str=None) -> webdriver.Chrome: global gBrowser lResourcePathStr = os.path.abspath(os.path.join(sys.executable, "..","..")) # Путь по умолчанию к портативному браузеру и драйверу (если скачивался репозиторий pyOpenRPA if inDriverExePathStr == None: inDriverExePathStr = os.path.join(lResourcePathStr, "SeleniumWebDrivers", "Chrome", "chromedriver_win32 v84.0.4147.30", "chromedriver.exe") if inChromeExePathStr == None: inChromeExePathStr = os.path.join(lResourcePathStr, "WChrome64-840414730", "App", "Chrome-bin", "chrome.exe") if inExtensionPathList == None: inExtensionPathList = [] # Set full path to exe of the chrome lWebDriverChromeOptionsInstance = webdriver.ChromeOptions() lWebDriverChromeOptionsInstance.binary_location = inChromeExePathStr #lWebDriverChromeOptionsInstance2 = webdriver.ChromeOptions() if inProfilePathStr is not None: inProfilePathStr = os.path.abspath(inProfilePathStr) lWebDriverChromeOptionsInstance.add_argument(f"user-data-dir={inProfilePathStr}") # Add extensions for lExtensionItemFullPath in inExtensionPathList: lWebDriverChromeOptionsInstance.add_extension (lExtensionItemFullPath) #if inDriverExePathStr == "built-in": # Run with specified web driver path gBrowser = webdriver.Chrome(executable_path = inDriverExePathStr, options=lWebDriverChromeOptionsInstance) #else: # lWebDriverInstance = webdriver.Chrome(options = lWebDriverChromeOptionsInstance) return gBrowser def BrowserChange(inBrowser): global gBrowser gBrowser = inBrowser def PageOpen(inURLStr): global gBrowser if gBrowser is not None: gBrowser.get(inURLStr) def PageScrollTo(inVerticalPxInt=0, inHorizontalPxInt=0): PageJSExecute(inJSStr=f"scroll({inHorizontalPxInt},{inVerticalPxInt})") def PageJSExecute(inJSStr): global gBrowser if gBrowser is not None: gBrowser.execute_script(inJSStr) def BrowserClose(): global gBrowser if gBrowser is not None: gBrowser.quit() def UIOByCSSList(inCSSStr, inUIO=None) -> list: if inUIO is None: global gBrowser if gBrowser is not None: gBrowser.find_elements_by_css_selector(css_selector=inCSSStr) else: inUIO.find_elements_by_css_selector(css_selector=inCSSStr) def UIOByCSSFirst(inCSSStr, inUIO=None) -> list: lResult = None lUIOList = UIOByCSSList(inCSSStr=inCSSStr, inUIO=inUIO) if len(lUIOList) > 0: lResult = lUIOList[0] return lResult def UIOTextGet(inUIO) -> str: return inUIO.text def UIOAttributeGet(inUIO, inAttributeStr) -> str: return inUIO.get_attribute(inAttributeStr) def UIOClick(inUIO): inUIO.click() def UIOSelectorClick(inUIOSelectorStr: str): PageJSExecute(inJSStr=f"document.querySelector('{inUIOSelectorStr}').click()") def UIOSelectorWaitAppear(inUIOSelectorStr, inWaitSecFloat:float=UIO_WAIT_SEC_FLOAT, inWaitIntervalSecFloat:float = UIO_WAIT_INTERVAL_SEC_FLOAT): lStartSecFloat = time.time() lResultList=[] while time.time() - lStartSecFloat < inWaitSecFloat: lResultList = UIOByCSSList(inCSSStr=inUIOSelectorStr) if len(lResultList)>0: break time.sleep(inWaitIntervalSecFloat) return lResultList def UIOWaitDisappear(inUIOSelectorStr, inWaitSecFloat:float=UIO_WAIT_SEC_FLOAT, inWaitIntervalSecFloat:float = UIO_WAIT_INTERVAL_SEC_FLOAT): lStartSecFloat = time.time() lResultList=[] while time.time() - lStartSecFloat < inWaitSecFloat: lResultList = UIOByCSSList(inCSSStr=inUIOSelectorStr) if len(lResultList)==0: break time.sleep(inWaitIntervalSecFloat) return lResultList