【问题标题】:xpath for accessing JS generated class instance用于访问 JS 生成的类实例的 xpath
【发布时间】:2011-12-07 15:54:42
【问题描述】:

我需要使用 xpath 作为 Selenium Webdriver 的定位器才能单击按钮。它位于动态生成的对话框中。 Firebug/firepath 为 div 引用提供了会改变的数字。我在这里阅读了很多很棒的提示并且很接近但似乎无法获得确切的规格。我需要 xpath 来访问关闭和取消:

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">
<button class="ui-button ui-widget ui-state-default ui-corner-all 
ui-button-text-only  ui-state-hover" type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Close</span>
</button>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only"
type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Create</span>

两者都没有成功:

    xpath="//*[@class='ui-button-text' and @value='Close'") 
    xpath="//span[contains(@class='ui-button-text' and @value='Close')]")

【问题讨论】:

    标签: xpath webdriver


    【解决方案1】:

    使用

    //span[@class = 'ui-button-text' and . = 'Close']
    

    这些选择XML文档中的所有span元素,字符串值为"Close",其class属性的字符串值为"ui-button-text"

    【讨论】:

    • 非常感谢。我不再收到无效的 xpath 错误,但它仍然无法访问该元素。看起来是因为该类有多个对话框并关闭,尽管只有一个处于活动状态。我正在尝试在跨度之前添加类似的内容: //[@class='ui-dialog'][style:display()!='none'] ...如果我的语法正确,这会起作用吗?其他建议?
    • @user1083649:style:sisplay() 是可用于 XPath 引擎的扩展函数吗?如果不是(我猜是这种情况),那么您将因此得到相应的错误。您必须能够仅使用其在 XML 文档中的属性和关系来唯一标识您想要选择的元素——否则您不知道您在问什么...
    • 再次感谢 - 扩展功能不可用,你的笔记让我回到了正确的轨道。我最终使用兄弟姐妹到达正确的区域: xpath="//div[@id='about-app-dialog']/../div[@class='ui-dialog-buttonpane ui-widget-内容 ui-helper-clearfix']/button/span[@class= 'ui-button-text' and . = 'OK']"
    【解决方案2】:

    使用@Dimitre 的表达方式:

    //span[@class='ui-button-text' and .='Close']
    

    我回答是为了解释你原来的表达方式哪里出了问题。

    第一个表达式

    //*[@class='ui-button-text' and @value='Close'")]
    

    这将选择文档 (//) 中任何位置的所有元素 (*),其属性 (@) 名为 class,其值为 ui-button-text,属性名为 value,其值为Close。属性轴说明符是@ 符号。这是attribute:: 的缩写。以下表达式是等价的:

    //*[@class='ui-button-text' and @value='Close'")]
    //*[attribute::class='ui-button-text' and attribute::value='Close'")] 
    

    以上表达式可以完全展开为:

    /descendant-or-self::node()/child::*[attribute::class='ui-button-text' and 
                                         attribute::value='Close'")] 
    

    简而言之:在构造表达式时尝试理解 XPath 的语法缩写

    第二个表达方式

    //span[contains(@class='ui-button-text' and @value='Close')]
    

    XPath contains function 具有以下签名:

    boolean contains(string, string)
    

    ...并且在规范中是这样描述的:

    如果第一个参数字符串,contains 函数返回 true 包含第二个参数字符串,否则返回 false。

    您似乎试图将其视为一个更通用的魔术函数,用于检查元素是否“包含”某些属性,但它本质上是一个 string 函数。

    我建议快速(或不那么快)阅读 XPath 1.0 建议:

    在那之前你只是在猜测。

    【讨论】:

    • 感谢您的解释 - 我已阅读 xpath 建议,但显然很难将其付诸实践。不幸的是,即使是“正确”的表达方式也没有给我一个进入网络元素的钩子。我认为它一定是点击了一个不活动的不同对话框按钮,我需要在路径中添加一个父级,以便在规范 Dimitre 帮助之前获得显示不是无的类。跨度(@class)之前的 xpath="//[@class='ui-dialog'][style:display()!='none'] 之类的东西。这听起来在正确的轨道上吗?
    猜你喜欢
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2011-03-10
    • 1970-01-01
    相关资源
    最近更新 更多