获取元素值

从 Web 页面获取元素值。

命令可用性: 本地 IBM RPA SaaS 和 IBM RPA

脚本语法

IBM RPA 的专有脚本语言具有类似于其他编程语言的语法。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。

webGet --selector(WebFieldElementSelectors) --id(String) --name(String) --css(String) --xpath(String) [--simulatehuman(Boolean)] [--timeout(TimeSpan)] (String)=value

依赖关系

使用 启动浏览器 (webStart) 命令来启动浏览器,或者在使用此命令之前使用 查找 Internet Explorer 浏览器 (findBrowser) 命令来连接 Microsoft Internet Explorer 。

输入参数

下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。

设计器方式标签 脚本方式名称 必需的 接受的变量类型 描述
选择器类型 selector Required WebElementSelectors 用于标识 Web 页面中的元素的选择器类型。

请参阅 selector 参数选项 以获取更多详细信息。
元素标识 id Required when the Selector type parameter is Id, IdAndName Text 要匹配并获取元素值的标识属性值。
元素名称 name Required when the Selector type parameter is Name, IdAndName Text 要匹配并获取元素值的 Name 属性值。
元素 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 秒。

selector 参数选项

下表显示了可用于 selector 输入参数的选项。 该表显示了在脚本方式下工作时的可用选项以及在设计器方式下的等效标签。

设计器方式标签 脚本方式名称 描述
标识 Id 根据"标识"属性值与元素匹配。
名称 Name 根据"名称"属性值与元素匹配。
Css CssSelector 基于 CSS 选择器值与元素匹配。
XPath XPath 根据 HTML 根中的 XML 路径与元素匹配。
标识和名称 IdAndName 根据标识和名称属性值与元素匹配。

输出参数

设计器方式标签 脚本方式名称 接受的变量类型 描述
value Text 返回从元素获取的值。

示例

示例 1: 以下代码示例演示如何从 Web 页面获取元素的值。 该命令打开浏览器,浏览到 Web 站点,使用 Css 选择器获取指定元素中的值,然后记录该元素的值。

defVar --name elementText --type String
defVar --name webpage --type String --value "http://www.example.com/"
defVar --name browser --type Browser
// Launches the Google Chrome browser
webStart --name browser --type "Chrome" --userprofilepreferences "AutomationOptimized" browser=value
// Navigates to the web page with the text elements
webNavigate --url "${webpage}"
webGet --selector "CssSelector" --css cssValue --simulatehuman  --timeout "00:00:10" elementText=value
// Logs the element value
logMessage --message "The element return: ${elementText}" --type "Info"
webClose --name browser

示例 2: 在此示例中,该命令使用其他选择器来获取指定元素的值,在此情况下,它使用 XPath 选择器。 由于此元素不是唯一的,并且它位于 iframe 中,因此 XPath 值必须解决命令将获取的元素。

defVar --name elementText --type String
defVar --name webpage --type String --value "http://www.example.com/"
defVar --name xpathSelector --type String --value "//iframe[@name=\'example\']\r\n"
// Launches the Google Chrome browser
webStart --name browser --type "Firefox" --userprofilepreferences "UserProfileOptmized" 
// Navigates to the web page with the text elements
webNavigate --url "${webpage}"
webGet --selector "XPath" --xpath "${xpathSelector}" --timeout "00:00:10" elementText=value
// Logs the element value
logMessage --message "The element return: ${elementText}" --type "Info"
webClose --name browser

限制

使用 IFrame 中的 XPath 或 CSS 选择器与不唯一的 Web 元素进行交互时,需要指定该命令将与 IFrame 中的哪个元素进行交互。 否则,将仅查找第一个元素。