【问题标题】:RobotFramework: xpath's href seen as "not a valid XPath expression."RobotFramework:xpath 的 href 被视为“不是有效的 XPath 表达式”。
【发布时间】:2017-12-26 07:55:43
【问题描述】:

第 1 行确实找到了选择器

Wait Until Element Is visible  xpath=//a[contains(text(),'Download selected certificate')]

然后当我尝试获取 href 元素时

${url}=    Get Element Attribute   xpath=//a[contains(text(),'Download selected certificate')]/@href

失败了 错误说

InvalidSelectorException:消息:无效选择器:由于以下错误,无法使用 xpath 表达式 //a[contains(text(),'Download selected certificate')]/ 定位元素: SyntaxError: 无法对“文档”执行“评估”:字符串 '//a[contains(text(),'Download selected certificate')]/' 不是有效的 XPath 表达式。

我不知道为什么,因为我有一个使用类似语法的不相关示例

${url}=    Get Element Attribute   xpath=//tr[contains(b/span, Jul)][${row_number}]/td[5]/span/a/@href

【问题讨论】:

    标签: xpath robotframework


    【解决方案1】:

    在 xpath 中,//a[contains(text(),'Download selected certificate')]/@href 的值将返回一个对象,该对象是该 a 标记的 href 属性。

    但在 Selenium2Library 中,当调用 Get Element Attribute 时,@ 指定属性名称以获取特定 dom elelement 的值。
    因此,该关键字的作用是在最后一个 @ 上拆分该字符串,在第一部分获取一个 web 元素,并检索目标属性(拆分的第二部分)的值。
    这样做,它最终会得到一个元素的定位器 //a[contains(text(),'Download selected certificate')]/ - 而尾随的 / 使其成为无效的 xpath。

    解决方法很简单——去掉斜线;例如:

    ${url}=    Get Element Attribute   xpath=//a[contains(text(),'Download selected certificate')]@href
    

    至于为什么你的最后一个样本有效 - 它也击败了我 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-03
      • 1970-01-01
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      • 2018-11-25
      • 2017-10-12
      相关资源
      最近更新 更多