【问题标题】:xpath or css needed for following html snippet以下 html 片段需要 xpath 或 css
【发布时间】:2011-09-03 11:12:02
【问题描述】:

以下 html 代码会生成一个弹出窗口以添加产品。试图找到可以在 selenium 中工作的 xpath 或 css,允许单击“添加”按钮添加新产品。

我尝试使用这个 xpath //div[18]/div[2]/div/button 仅适用于一个会话。当您关闭浏览器并重新打开时,上面的 xpath 不起作用。那么,在这种情况下,我们如何编写一个不必传递 div[number] 的 xpath 是 div[18]。

<div style="height: auto; min-height: 104px; width: auto;" class="dialog ui-dialog-content ui-widget-content">
<fieldset><legend>search</legend>
<div id="nameorids">
<input type="radio">name
<input type="radio">id
</fieldset>
<div style="clear: both; padding-top: 5px; float: right;"><button class="add">add</button></div></div>

【问题讨论】:

    标签: css xpath selenium


    【解决方案1】:

    首先,这个 HTML 无效:nameorids div 在关闭字段集之前没有关闭。错字?

    如果您可以影响 HTML,请尝试将 id 或 class 属性添加到外部 div,以便您可以使用 css 选择器定位它们。例如。如果你能让 HTML 看起来像

    <div id="foo" ...
    

    然后你可以使用选择器#foo button.add

    【讨论】:

    • 嗨 Orip--感谢您的回复。不幸的是,我无权修改 HTML 代码。所以我不能分配任何标识或任何属性。
    【解决方案2】:

    同一节点有许多不同的可能 XPath。在这种情况下,这是唯一具有“添加”类的按钮吗?在这种情况下:

    //button[contains(concat(' ', @class, ' '), ' add ')]
    

    只会得到那个按钮。仅包含 HTML 的 sn-p 不是很有用。

    如果您可以访问该页面,并且可以运行 Chrome 浏览器,请试用我的扩展程序 here。安装后重新启动 Chrome,然后右键单击按钮并选择PsychoXPath -&gt; Element (Short)。这将尝试找到该元素的唯一路径,并将其复制到剪贴板和控制台。

    【讨论】:

      【解决方案3】:

      css 是 button.add

      selenium.click("css=button.add");
      

      【讨论】:

        猜你喜欢
        • 2017-04-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-04
        相关资源
        最近更新 更多