将焦点置于 Web 元素上

让 Web 页面上的某个元素获得焦点。

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

脚本语法

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

webFocus --selector(WebElementSelectors) --id(String) --name(String) --css(String) --linktext(String) --innertext(String) --tagname(String) --xpath(String) --class(String) --elementvalue(String) [--simulatehuman(Boolean)] [--timeout(TimeSpan)] (Boolean)=value

依赖关系

在使用将焦点放在 Web 元素上 (webFocus) 命令之前,请使用启动浏览器 (webStart) 命令来启动浏览器,或使用查找 Internet Explorer 浏览器 (findBrowser) 命令来连接 Microsoft Internet Explorer。

输入参数

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

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

请参阅 selector 参数选项
标识 id Required when the Search for parameter is Id, IdAndName Text 包含标识属性值的文本,以与元素匹配且将焦点置于元素上。
元素名称 name Required when the Search for parameter is Name, NameAndValue, IdAndName Text 包含名称属性值的文本,以与元素匹配且将焦点置于元素上。
CSS css Required when the Search for parameter is CssSelector Text 包含 CSS 选择器的文本,以与元素匹配并将焦点置于元素上。
链接文本 linktext Required when the Search for parameter is LinkText Text 包含链接文本值的文本,以与元素匹配且将焦点置于元素上。
文本 innertext Required when the Search for parameter is InnerTextAndTag Text 包含元素文本内容的文本,以与元素匹配且将焦点置于元素上。
标记名称 tagname Required when the Search for parameter is InnerTextAndTag Text 包含标记名称属性值的文本,以与元素匹配且将焦点置于元素上。
XPath xpath Required when the Search for parameter is XPath Text 包含 XML 路径值的文本,以与元素匹配且将焦点置于元素上。
class Required when the Search for parameter is ClassAndValue Text 包含类属性值的文本,以与元素匹配且将焦点置于元素上。
元素值 elementvalue Required when the Search for parameter is ClassAndValue, NameAndValue Text 包含“值”属性值的文本,以与元素匹配且将焦点置于元素上。
模拟人类 simulatehuman Optional Boolean 此参数不会更改命令的行为。
Timeout timeout Optional Time Span, Number, Text 运行命令时的最长等待时间。 如果为空白,那么缺省超时为 5 秒。

selector 参数选项

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

设计器方式标签 脚本方式名称 Description
标识 Id 根据"标识"属性值与元素匹配。
名称 Name 根据"名称"属性值与元素匹配。
Css CssSelector 基于 CSS 选择器值与元素匹配。
链接文本 LinkText 根据链接文本属性值与元素匹配。
内部文本和控件类型 InnerTextAndTag 根据元素文本内容和标记名称属性值与元素匹配。
XPath XPath 根据 HTML 根中的 XML 路径与元素匹配。
类和值 ClassAndValue 根据类名和值属性与元素匹配。
名称和值 NameAndValue 根据名称和值属性与元素匹配。
标识和名称 IdAndName 根据标识和名称属性值与元素匹配。

输出参数

设计器方式标签 脚本方式名称 接受的变量类型 Description
成功 value Boolean 如果元素成功获得焦点,那么返回 true,否则返回 false

示例

以下代码示例演示如何将焦点置于 Web 页面的元素上。 Web 元素获得焦点 (webFocus) 命令使用 id 选择器将焦点置于两个不同的输入字段上。

defVar --name webpage --type String --value "https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_input_test"
defVar --name instanceName --type String --value "chrome-browser"
// Launches the Google Chrome browser
webStart --name "${instanceName}" --type "Chrome"
// Navigates to the web page with the input fields
webNavigate --url "${webpage}"
webFocus --selector "Id" --id fname
webFocus --selector "Id" --id lname
webClose --name "${instanceName}" --leavebrowseropen

限制

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