背景
RPA工作流中最常见的场景是操作浏览器,对页面内容进行相关操作。本例中以 http://wufazhuce.com/one/2558 页面为例。带领大家初步探索如何使用RPA 工具进行自动化抓取页面文本内容。
本文开发RPA脚本会用到JavaScript语言。这里所用到的RPA工具LeanRunner,可从Windows的应用商店上直接下载,它可以支持使用node.js的开源自动化库做RPA开发。用户可以按照下面操作步骤,逐步实现自己的RPA脚本。
操作步骤
新建项目
打开LeanRunner, 选择【项目】--【新建】--【选择基本项目模板】,输入项目名称:demo,选择项目路径:
安装依赖库
selenium-webdriver 是流行的操作Web自动化库,使用chromedriver库可以驱动Chrome实现各类网页自动化,当然文字的提取也不在话下,本次RPA就是利用这两个库来实现功能。所以创建好项目之后需要安装对应的库。
点击LeanRunner打开命令行工具按钮 ,执行安装命令:
npm init -ynpm install chromedriver selenium-webdriver @types/selenium-webdriver --save
备注:npm作为node.js的包管理机制,需要安装node.js环境才能使用
(下载地址:https://nodejs.org/en/)
定义流程步骤
定义流程步骤是为了让自动化流程有好的可读性。
a. 打开 main.js,在【工具箱】--【框架】中找到 stepGroup 方法,拖拽到js 文件中。
b. 在弹出的对话框中 输入描述文本: 抓取网页文本内容
并点击插入代码。
c. 此时,main.js 的文件内容:
const { stepGroup } = require('leanrunner');async function main() { await stepGroup("抓取网页文本内容", async () => { })}
d. 继续拖拽【工具箱】--【框架】中的step 方法,描述文本输入:使用Chrome浏览器打开要抓取的网站:
e. 按照上面操作步骤,再次插入 抓取文本
和 关闭浏览器
的步骤定义。
main.js如下:
const { step } = require('leanrunner');const { stepGroup } = require('leanrunner');async function main() { await stepGroup("抓取网页文本内容", async () => { await step("使用Chrome浏览器打开要抓取的网站", async (world) => { }) await step("抓取文本", async (world) => { }) await step("关闭浏览器", async (world) => { }) })}
f. 插入Workflow.run函数, RPA执行最终要执行,选择【工具箱】--【Framework】中的Workrun.run()函数:
g. 运行的函数中 输入“main”:
最终代码为:
const { Workflow } = require('leanrunner');const { step } = require('leanrunner');const { stepGroup } = require('leanrunner');async function main() { await stepGroup("抓取网页文本内容", async () => { await step("使用Chrome浏览器打开要抓取的网站", async (world) => { }) await step("抓取文本", async (world) => { }) await step("关闭浏览器", async (world) => { }) })}Workflow.run(main);
实现操作步骤
参考 selenium-webdriver API
(https://seleniumhq.github.io/selenium/docs/api/javascript/index.html). 分别实现上述操作步骤:
a. 使用Chrome浏览器打开要抓取的网站:
const WebDriver = require('selenium-webdriver');let driver = new WebDriver.Builder().forBrowser('chrome').build();const url = 'http://wufazhuce.com/one/2558';await driver.get(url);
上述代码创建了一个WebDriver实例,打开浏览器窗口,并导航到目标url。
b. 抓取文本:
let text = await driver.findElement({ css:'div[]'}).getText();console.log(text);
上面代码用css选择器定位到要访问的元素,并打印输出。
c. 关闭浏览器
await driver.close();
最终实现的代码如下:
const { Workflow } = require('leanrunner');const { step } = require('leanrunner');const { stepGroup } = require('leanrunner');require('chromedriver');const WebDriver = require('selenium-webdriver');let driver = new WebDriver.Builder().forBrowser('chrome').build();async function main() { await stepGroup("抓取网页文本内容", async () => { await step("使用Chrome浏览器打开要抓取的网站", async (world) => { const url = 'http://wufazhuce.com/one/2558'; await driver.get(url); }) await step("抓取文本", async (world) => { let text = await driver.findElement({ css:'div[]'}).getText(); console.log(text); world.attachText(text); }) await step("关闭浏览器", async (world) => { await driver.close() }) })}Workflow.run(main);
执行
点击“运行”按钮,或者点击“运行项目”按钮
可以看到浏览器打开网页并将文本内容打印在LeanRunner设计器的输出面板。
如果是运行项目,还会显示html的运行报告:
对用户来说,html报告有较强的可读性。
总结
至此,我们完成了一个基本操作网页的RPA。后续可以在此RPA基础上进一步加深操作,比如抓取的文本内容存放到Excel表格中,或者存放到数据库中。
本文用到的selenium-webdriver自动化库,是非常流行的开源库,能够支持各种类型的浏览器,并能及时更新,支持最新版本的浏览器。同时Node.js也是非常流行的开源平台。基于这样一些技术开发RPA自动化脚本,保持了RPA脚本的可用性和可维护性,结合LeanRunner RPA平台,能够帮助企业,快速打造您自己的流程自动化。