首页 文章

RobotFramework:xpath的href被视为“不是有效的XPath表达式” .

提问于
浏览
0

第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:无法执行'evaluate'on' Document':字符串'// a [contains(text(),'Download selected certificate')] /'不是有效的XPath表达式 .

我不知道为什么因为我有一个与语法类似的工作无关的例子

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

1 回答

  • 1

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

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

    解决方案很简单 - 丢失尾随斜线;例如 . :

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

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

相关问题