从 Web 中获取图像
从 Web 页面中获取图像。
命令可用性: 本地 IBM RPA SaaS 和 IBM RPA
Description
获取 Web 页面中某个元素的图像。 该命令将图像存储在 IBM RPA Studio 的内存流中,并在脚本运行时期间返回将此图像作为 .png 文件包含在内的 Image 值。
脚本语法
IBM RPA 的专有脚本语言具有类似于其他编程语言的语法。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。
webGetImage --selector(WebFieldElementSelectors) --id(String) --name(String) --css(String) --xpath(String) [--simulatehuman(Boolean)] [--timeout(TimeSpan)] (Image)=value
依赖关系
在使用从 Web 获取图像 (webGetImage) 命令之前,请使用启动浏览器 (webStart) 命令启动浏览器。 或者,使用查找 Internet Explorer 浏览器 (findBrowser) 命令连接 Microsoft™ Internet Explorer。
输入参数
下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。
| 设计器方式标签 | 脚本方式名称 | 必需的 | 接受的变量类型 | Description |
|---|---|---|---|---|
| 选择器类型 | selector |
Required |
WebFieldElementSelectors |
用于标识 Web 页面中元素的选择器类型。 请参阅 selector 参数选项。 |
| 图像标识 | id |
Required when the Selector type parameter is Id, IdAndName |
Text |
包含标识属性值的文本,以与元素匹配并获取元素。 |
| 图像名称 | name |
Required when the Selector type parameter is Name, IdAndName |
Text |
包含名称属性值的文本,以与元素匹配并获取元素。 |
| 图像 CSS | css |
Required when the Selector type parameter is CssSelector |
Text |
包含 CSS 选择器的文本,以与元素匹配并获取元素。 |
| XPath | xpath |
Required when the Selector type parameter is XPath |
Text |
包含 XML 路径值的文本,以与元素匹配并获取元素。 |
| 模拟人类 | simulatehuman |
Optional |
Boolean |
此参数不会更改命令的行为。 |
| Timeout | timeout |
Optional |
Time Span, Number, Text |
运行命令时的最长等待时间。 如果为空白,那么缺省超时为 5 秒。 |
| 强制启用滚动 | forcescroll |
Optional when the Get Image From Full Page Scrennshot parameter is True |
Boolean |
启用以更改页面的源代码,从而启用主体滚动。 此选项会将元素更改为自动调整其高度,以便内容完全显示在屏幕上,且可以正确剪切元素。 此更改可能会影响命令运行时页面的外观。 由于此原因,页面结构中的某些元素可能无法正确显示。 |
| 从完整页面屏幕快照获取图像 | fromfullpage |
Optional |
Boolean |
如果无法在预览窗口中自由显示完整图像,请启用此项以获取该图像。 |
selector 参数选项
下表显示了可用于 selector 输入参数的选项。 该表显示了在脚本方式下工作时的可用选项以及在设计器方式下的等效标签。
| 设计器方式标签 | 脚本方式名称 | Description |
|---|---|---|
| 标识 | Id |
根据"标识"属性值与元素匹配。 |
| 名称 | Name |
根据"名称"属性值与元素匹配。 |
| Css | CssSelector |
基于 CSS 选择器值与元素匹配。 |
| XPath | XPath |
根据 HTML 根中的 XML 路径与元素匹配。 |
| 标识和名称 | IdAndName |
根据标识和名称属性值与元素匹配。 |
输出参数
| 设计器方式标签 | 脚本方式名称 | 接受的变量类型 | Description |
|---|---|---|---|
| Image | value |
Image |
返回一个 Image 值,该值包含从 Web 页面获取的图像。 |
示例
以下代码示例演示了如何使用从 Web 获取图像 (webGetImage) 命令从 css 选择器获取图像。
defVar --name desktopPath --type String
defVar --name webImage --type Image
defVar --name imagePath --type String
defVar --name webpage --type String --value "https://www.ibm.com/docs/en/rpa/21.0"
defVar --name elementSelector --type String --value "#app > div > div > div > div > div.bx--masthead > div > header > div.bx--header__logo.bx--search-active > a > svg"
// Gets a folder path of the system. This folder path stores the image
getSpecialFolder --folder "Desktop" desktopPath=value
// Launches the Google Chrome browser
webStart --name browser --type "Chrome"
// Navigates to the web page
webNavigate --url "${webpage}"
// Waits until the element be available on the web page
webWaitElement --selector "CssSelector" --css "${elementSelector}"
// This command stores the image in memory during the script runtime
webGetImage --selector "CssSelector" --css "${elementSelector}" --simulatehuman webImage=value
// Saves the image on the desktop
saveImage --image ${webImage} --directory "${desktopPath}" --file ibm_logo --overwrite --format "Png" imagePath=value
// Logs the image file path
logMessage --message "Image path: ${imagePath}" --type "Info"
webClose --name browser
限制
- 当使用 XPath 或 CSS 选择器与不唯一的 Web 元素进行交互时,即在 IFrame 内,需要指定命令将与 IFrame 内的哪个元素进行交互。 否则,将仅查找第一个元素。