6

使用Robot机器人框架实现自动化操作

 3 years ago
source link: https://www.jdon.com/59450
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

使用Robot机器人框架实现自动化操作

RPA只是一种自动执行重复性任务的方法,这些任务通常由人类使用软件机器人完成。这些通常是在 Web 浏览器、MS Excel、电子邮件客户端等图形用户界面 (GUI) 中完成的任务……

开源RPA项目有Robot FrameworkTagUI,Robot Framework 拥有最多的 GitHub 明星,并得到广泛社区的支持。是 Python 之上的一层非常酷,因此很容易将自定义 Python 代码插入到机器人中。

Robot Framework是一个用python语言开发的自动化测试框架。机器人框架支持大多数类型的功能测试,例如用户界面测试(UI)、后端测试(如 REST 服务测试)甚至数据库测试。

Robot框架把你的典型编程语言的功能称为关键字。我们将定义一些关键字,以便我们能够抽象出一些你在浏览器中处理网站时遇到的粗糙边缘。特别是,网站是异步加载许多元素的野兽,这意味着元素可能还不可见或未启用。

机器人的速度本来就比人类快得多,所以如果你不小心,你的机器人很容易因为用户界面尚未准备好或因为其他竞赛条件的出现而出错。

当然,你可以在你的代码中插入睡眠语句,这将模拟人类会遇到的时间,但这是一种反模式,因为:

  • 添加sleep睡眠语句会使你的机器人变得更慢
  • 时间可以根据网络、后端服务器、甚至你的计算能力而变化。

相反,我们将创建一些辅助关键字,等待元素的可见和启用,甚至在页面加载时间过长或发生意外错误时引入一个重试循环。

这个关键字是:Wait Until Element Ready

Wait Until Element Ready Inner
    [Arguments]    ${locator}    ${timeout}=None

    Wait Until Page Contains Element    ${locator}    ${timeout}
    Wait Until Element Is Visible    ${locator}
    Wait Until Element Is Enabled    ${locator}
    Scroll Element Into View    ${locator}
    
    Wait Until Element Is Visible    ${locator}
    Wait Until Element Is Enabled    ${locator}
    Scroll Element Into View    ${locator}

Wait Until Element Ready
    [Arguments]    ${locator}    ${timeout}=None
    Wait Until Keyword Succeeds    3   1s    Wait Until Element Ready Inner    ${locator}    ${timeout}

接下来,我们将定义Click Element When Ready,它是使用Wait Until Element Ready,然后点击该元素。

Click Element When Ready Inner
    [Arguments]    ${locator}
    Wait Until Element Ready    ${locator}
    ${element}=    Get WebElement     ${locator}
    Click Element    ${element}
    [Return]    ${element}

Click Element When Ready
    [Arguments]    ${locator}
    Wait Until Keyword Succeeds    3   1s    Click Element When Ready Inner    ${locator}

为了使我们能够恢复同一个浏览器会话,这将有助于我们避免Todoist在每次登录时向我们发送电子邮件,我们将借用这篇文章中的一个技巧。

Open Browser Profiled
    [Arguments]    ${url}
    ${options}=    Evaluate    sys.modules['selenium.webdriver'].ChromeOptions()    sys, selenium.webdriver
    Call Method    ${options}    add_argument    --user-data-dir\=${USER_DATA_PATH}
    Create WebDriver    Chrome    chrome_options=${options}
    Go To    ${url}

详细源码:github.com/redgeoff/grocery-shopping-bot

点击标题更多


Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK