You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ORPA-pyOpenRPA/Sources/pyOpenRPA/Robot/UIWeb.py

113 lines
4.9 KiB

from selenium import *
from selenium import webdriver, common
import os
import sys
from pyOpenRPA.Tools import CrossOS
from . import Screen
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: return gBrowser.execute_script(inJSStr)
def BrowserClose():
global gBrowser
if gBrowser is not None: gBrowser.quit()
def UIOSelectorList(inUIOSelectorStr, inUIO=None) -> list:
lResultList = []
if inUIO is None:
global gBrowser
if gBrowser is not None:
lResultList = gBrowser.find_elements_by_css_selector(css_selector=inUIOSelectorStr)
else: lResultList = inUIO.find_elements_by_css_selector(css_selector=inUIOSelectorStr)
return lResultList
def UIOSelectorFirst(inUIOSelectorStr, inUIO=None) -> list:
lResult = None
lUIOList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr, 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 UIOSelectorDraw(inUIOSelectorStr: str, inIsFirst:bool=False):
lList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr)
global gBrowser
lBrowserRect = gBrowser.get_window_rect()
lViewportHeightInt = PageJSExecute(inJSStr=f"return $(window).height();")
lBrowserRect["y"]= lBrowserRect["y"] + (lBrowserRect["height"] - lViewportHeightInt)
if inIsFirst == True and len(lList)>0: lList = [lList[0]]
for lItem in lList:
lRect = lItem.rect
lBox = Screen.BoxCreate(inTopInt=round(lRect['y']+lBrowserRect['y']), inLeftInt=round(lRect['x']+lBrowserRect['x']), inHeightInt=round(lRect['height']), inWidthInt=round(lRect['width']))
Screen.BoxDraw(inBox=lBox)
def UIOSelectorClick(inUIOSelectorStr: str):
PageJSExecute(inJSStr=f"document.querySelector('{inUIOSelectorStr}').click()")
def UIOSelectorWaitAppear(inUIOSelectorStr:str, inWaitSecFloat:float=UIO_WAIT_SEC_FLOAT, inWaitIntervalSecFloat:float = UIO_WAIT_INTERVAL_SEC_FLOAT):
lStartSecFloat = time.time()
lResultList=[]
while time.time() - lStartSecFloat < inWaitSecFloat:
lResultList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr)
if len(lResultList)>0: break
time.sleep(inWaitIntervalSecFloat)
return lResultList
def UIOSelectorWaitDisappear(inUIOSelectorStr:str, inWaitSecFloat:float=UIO_WAIT_SEC_FLOAT, inWaitIntervalSecFloat:float = UIO_WAIT_INTERVAL_SEC_FLOAT):
lStartSecFloat = time.time()
lResultList=[]
while time.time() - lStartSecFloat < inWaitSecFloat:
lResultList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr)
if len(lResultList)==0: break
time.sleep(inWaitIntervalSecFloat)
return lResultList