From 84fe68a54b146b05c857850fe3b09e4a8cf4051a Mon Sep 17 00:00:00 2001 From: Ivan Maslov Date: Mon, 18 Jul 2022 00:47:41 +0300 Subject: [PATCH] tested UIWeb --- Sources/pyOpenRPA/Robot/UIWeb.py | 97 +++++++++++++--- Utils/Jupyter-notebooks/Web.ipynb | 185 ++++++++++++++++++++++++++++++ 2 files changed, 268 insertions(+), 14 deletions(-) create mode 100644 Utils/Jupyter-notebooks/Web.ipynb diff --git a/Sources/pyOpenRPA/Robot/UIWeb.py b/Sources/pyOpenRPA/Robot/UIWeb.py index ca4692b1..15632342 100644 --- a/Sources/pyOpenRPA/Robot/UIWeb.py +++ b/Sources/pyOpenRPA/Robot/UIWeb.py @@ -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 inWaitSecFloat: raise Exception(f"Wait time is over. No element has been disappear") \ No newline at end of file + 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('')) + +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) + \ No newline at end of file diff --git a/Utils/Jupyter-notebooks/Web.ipynb b/Utils/Jupyter-notebooks/Web.ipynb new file mode 100644 index 00000000..fdedf862 --- /dev/null +++ b/Utils/Jupyter-notebooks/Web.ipynb @@ -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": [ + "\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 +}