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

142 lines
6.2 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 UIOAttributeStyleGet(inUIO, inAttributeStr) -> str:
return inUIO.get_attribute(inAttributeStr)
#TODO
#def UIOAttributeSet(inUIO, inAttributeStr) -> str:
#def UIOAttributeStyleSet(inUIO, inAttributeStr) -> str:
def UIOClick(inUIO):
inUIO.click()
def UIOSelectorHighlight(inUIOSelectorStr: str, inIsFirst:bool=False, inDurationSecFloat:float=3.0, inColorStr:str="green"):
global gBrowser
if inIsFirst == True:
lJSStr = \
f"var lElementList = document.querySelectorAll(\"{inUIOSelectorStr}\");" \
f"if (lElementList.length>0) {{ lElementList=[lElementList[0]]; }}" \
f"for (var lIndexInt=0; lIndexInt<lElementList.length;lIndexInt++) {{" \
f" lElement=lElementList[lIndexInt];" \
f" lElement.ORPABackupStyleOutline = lElement.style[\"outline\"];" \
f" lElement.style[\"outline\"]=\"2px solid {inColorStr}\";" \
f"}}" \
f"window.ORPAOutlineList = lElementList;"
PageJSExecute(inJSStr=lJSStr)
else:
lJSStr = \
f"var lElementList = document.querySelectorAll(\"{inUIOSelectorStr}\");" \
f"for (var lIndexInt=0; lIndexInt<lElementList.length;lIndexInt++) {{" \
f" lElement=lElementList[lIndexInt];" \
f" lElement.ORPABackupStyleOutline = lElement.style[\"outline\"];" \
f" lElement.style[\"outline\"]=\"2px solid {inColorStr}\";" \
f"}}" \
f"window.ORPAOutlineList = lElementList;"
PageJSExecute(inJSStr=lJSStr)
time.sleep(inDurationSecFloat)
lJSStr = \
f"var lElementList = window.ORPAOutlineList;" \
f"for (var lIndexInt=0; lIndexInt<lElementList.length;lIndexInt++) {{" \
f" lElement=lElementList[lIndexInt];" \
f" lElement.style[\"outline\"]=lElement.ORPABackupStyleOutline;" \
f"}}" \
f"delete window.ORPAOutlineList;"
PageJSExecute(inJSStr=lJSStr)
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)
if time.time() - lStartSecFloat > inWaitSecFloat: raise Exception(f"Wait time is over. No element has been appear")
return lResultList
def UIOSelectorWaitDisappear(inUIOSelectorStr:str, inWaitSecFloat:float=UIO_WAIT_SEC_FLOAT, inWaitIntervalSecFloat:float = UIO_WAIT_INTERVAL_SEC_FLOAT):
lStartSecFloat = time.time()
while time.time() - lStartSecFloat < inWaitSecFloat:
lResultList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr)
if len(lResultList)==0: break
time.sleep(inWaitIntervalSecFloat)
if time.time() - lStartSecFloat > inWaitSecFloat: raise Exception(f"Wait time is over. No element has been disappear")