tested UIWeb

dev-linux
Ivan Maslov 2 years ago
parent 42fdd6712b
commit 84fe68a54b

@ -3,9 +3,10 @@ from selenium import webdriver, common
import os
import sys
from pyOpenRPA.Tools import CrossOS
from . import Screen
import time
# XPATH CSS CHEAT CHEET: https://devhints.io/xpath
UIO_WAIT_SEC_FLOAT = 60
UIO_WAIT_INTERVAL_SEC_FLOAT = 1.0
@ -46,9 +47,10 @@ def PageOpen(inURLStr):
def PageScrollTo(inVerticalPxInt=0, inHorizontalPxInt=0):
PageJSExecute(inJSStr=f"scroll({inHorizontalPxInt},{inVerticalPxInt})")
def PageJSExecute(inJSStr):
def PageJSExecute(inJSStr, *inArgList):
# arguments[0], arguments[1] etc
global gBrowser
if gBrowser is not None: return gBrowser.execute_script(inJSStr)
if gBrowser is not None: return gBrowser.execute_script(inJSStr, *inArgList)
def BrowserClose():
global gBrowser
@ -59,8 +61,15 @@ def UIOSelectorList(inUIOSelectorStr, inUIO=None) -> list:
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)
if UIOSelectorDetect(inUIOSelectorStr=inUIOSelectorStr) == "CSS":
lResultList = gBrowser.find_elements_by_css_selector(css_selector=inUIOSelectorStr)
else:
lResultList = gBrowser.find_elements_by_xpath(xpath=inUIOSelectorStr)
else:
if UIOSelectorDetect(inUIOSelectorStr=inUIOSelectorStr) == "CSS":
lResultList = inUIO.find_elements_by_css_selector(css_selector=inUIOSelectorStr)
else:
lResultList = gBrowser.find_elements_by_xpath(xpath=inUIOSelectorStr)
return lResultList
def UIOSelectorFirst(inUIOSelectorStr, inUIO=None) -> list:
@ -76,13 +85,27 @@ def UIOAttributeGet(inUIO, inAttributeStr) -> str:
return inUIO.get_attribute(inAttributeStr)
def UIOAttributeStyleGet(inUIO, inAttributeStr) -> str:
return inUIO.get_attribute(inAttributeStr)
return inUIO.value_of_css_property(inAttributeStr)
#TODO
#def UIOAttributeSet(inUIO, inAttributeStr) -> str:
def UIOAttributeSet(inUIO, inAttributeStr, inValue):
lJSStr = \
f"arguments[0].setAttribute(arguments[1], arguments[2]);"
gBrowser.execute_script(lJSStr,inUIO, inAttributeStr, inValue)
#def UIOAttributeStyleSet(inUIO, inAttributeStr) -> str:
def UIOAttributeRemove(inUIO, inAttributeStr):
lJSStr = \
f"arguments[0].removeAttribute(arguments[1]);"
gBrowser.execute_script(lJSStr,inUIO, inAttributeStr)
def UIOAttributeStyleSet(inUIO, inAttributeStr, inValue):
lJSStr = \
f"arguments[0].style[arguments[1]]=arguments[2];"
gBrowser.execute_script(lJSStr,inUIO, inAttributeStr, inValue)
def UIOAttributeStyleRemove(inUIO, inAttributeStr):
lJSStr = \
f"arguments[0].style[arguments[1]]=\"\";"
gBrowser.execute_script(lJSStr,inUIO, inAttributeStr)
def UIOClick(inUIO):
inUIO.click()
@ -90,8 +113,9 @@ def UIOClick(inUIO):
def UIOSelectorHighlight(inUIOSelectorStr: str, inIsFirst:bool=False, inDurationSecFloat:float=3.0, inColorStr:str="green"):
global gBrowser
if inIsFirst == True:
lUIOList = [UIOSelectorFirst(inUIOSelectorStr=inUIOSelectorStr)]
lJSStr = \
f"var lElementList = document.querySelectorAll(\"{inUIOSelectorStr}\");" \
f"var lElementList = arguments[0];" \
f"if (lElementList.length>0) {{ lElementList=[lElementList[0]]; }}" \
f"for (var lIndexInt=0; lIndexInt<lElementList.length;lIndexInt++) {{" \
f" lElement=lElementList[lIndexInt];" \
@ -99,17 +123,18 @@ def UIOSelectorHighlight(inUIOSelectorStr: str, inIsFirst:bool=False, inDuration
f" lElement.style[\"outline\"]=\"2px solid {inColorStr}\";" \
f"}}" \
f"window.ORPAOutlineList = lElementList;"
PageJSExecute(inJSStr=lJSStr)
PageJSExecute(lJSStr, lUIOList)
else:
lUIOList = UIOSelectorList(inUIOSelectorStr=inUIOSelectorStr)
lJSStr = \
f"var lElementList = document.querySelectorAll(\"{inUIOSelectorStr}\");" \
f"var lElementList = arguments[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)
PageJSExecute(lJSStr, lUIOList)
time.sleep(inDurationSecFloat)
lJSStr = \
f"var lElementList = window.ORPAOutlineList;" \
@ -139,4 +164,48 @@ def UIOSelectorWaitDisappear(inUIOSelectorStr:str, inWaitSecFloat:float=UIO_WAIT
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")
if time.time() - lStartSecFloat > inWaitSecFloat: raise Exception(f"Wait time is over. No element has been disappear")
from lxml import etree
from io import StringIO
gXML = etree.parse(StringIO('<foo><bar></bar></foo>'))
def UIOSelectorDetect(inUIOSelectorStr:str) -> str:
"""Идентифицировать стиль селектора (CSS или XPATH)
:param inUIOSelectorStr: _description_
:type inUIOSelectorStr: str
:return: "CSS" или "XPATH"
:rtype: str
"""
global gXML
lResultStr = "CSS"
try:
gXML.xpath(inUIOSelectorStr)
lResultStr = "XPATH"
except etree.XPathEvalError as e:
lResultStr = "CSS"
return lResultStr
def UIOMouseSearchInit():
lJSStr = """
document.ORPASearch = function(e){
document.ORPAMouseXInt = e.clientX;
document.ORPAMouseYInt = e.clientY;
}
document.addEventListener('mousemove', document.ORPASearch, {
passive: true})
"""
PageJSExecute(lJSStr)
def UIOMouseSearchReturn():
lJSStr = """
document.removeEventListener('mousemove', document.ORPASearch);
return document.elementFromPoint(document.ORPAMouseXInt,document.ORPAMouseYInt);
"""
return PageJSExecute(lJSStr)

@ -0,0 +1,185 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"import os\n",
"sys.path.insert(0, os.path.abspath(\"..\\\\..\\\\Sources\"))\n",
"from pyOpenRPA.Robot import UIWeb\n",
"UIWeb.BrowserChromeStart()\n",
"UIWeb.PageOpen(\"https://mail.ru\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"lUIOSelectorStr = \"#grid > div.grid-middle > div.grid__main-col.svelte-2y66pa > div.grid_newscol.grid_newscol__more-pulse.svelte-1yvqfic > div.grid__ccol.svelte-1yvqfic > ul > li:nth-child(5) > div > a\"\n",
"lUIOSelectorStr = \"//*[@id=\\\"grid\\\"]/div[2]/div[2]/div[3]/div[1]/ul/li[5]/div/a\"\n",
"UIWeb.UIOSelectorHighlight(lUIOSelectorStr)\n",
"lUIO=UIWeb.UIOSelectorList(lUIOSelectorStr)[0]"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.BrowserClose()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.UIOAttributeSet(lUIO,\"test\", \"asd\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.UIOAttributeRemove(lUIO, \"test\")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.UIOAttributeStyleSet(lUIO, \"color\", \"grey\")"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.UIOAttributeStyleRemove(lUIO, \"color\")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'rgba(0, 91, 209, 1)'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"UIWeb.UIOAttributeStyleGet(lUIO, \"color\")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"UIWeb.UIOMouseSearchInit()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<selenium.webdriver.remote.webelement.WebElement (session=\"ee4349205244596feacecc29a381864a\", element=\"754d0f45-e153-4c44-987b-35fbe16646a3\")>\n"
]
}
],
"source": [
"print(UIWeb.UIOMouseSearchReturn())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading…
Cancel
Save