【问题标题】:Unable to find an element using CSS selector in CasperJS无法在 CasperJS 中使用 CSS 选择器找到元素
【发布时间】:2016-03-24 13:29:48
【问题描述】:

我正在使用 casperjs 编写一些测试用例。我想在 TR 中找到一个 TD 元素。

  <tr class="x-boundlist-item selectorCls">
        <td class="x-boundlist-item-td selectorTdCls" width="70%">64Columns</td>
       <td class="x-boundlist-item-td" align="right" style="padding-right:4px;">
               <a href="#" style="color:#15428B;"></a>
      </td>
 </tr>

我想通过css 类x-boundlist-item selectorCls 找到一个TR 元素 然后在这里面,我需要找到 first TD 元素并触发点击事件。

我使用了.waitForSelector(".x-boundlist-item-td selectorTdCls",,它在一段时间后超时并且没有返回任何内容。

我也尝试过使用var x = document.body.querySelectorAll('.x-boundlist-item-td selectorTdCls'); 甚至没有奏效。

【问题讨论】:

    标签: css xpath css-selectors phantomjs casperjs


    【解决方案1】:

    x-boundlist-item-td 和 selectorTdCls 是同一元素上的两个类,因此选择器应为 .x-boundlist-item-td.selectorTdCls

    .x-boundlist-item-td selectorTdCls 会匹配类似...

    <div class="x-boundlist-item-td">
      <selectorTdCls></selectorTdCls> <!-- << matches this -->
    </div>
    

    【讨论】:

    • 啊哈,我在浏览器中尝试了querySelectorAll,我需要将您的示例包装在 ....
      中以使其正常工作。添加后它对你有用吗?
    • 是的..它包裹在桌子下面。你能告诉我你用来选择元素的确切代码吗?
    • 我有一个包含 &lt;html&gt;&lt;body&gt;&lt;table&gt;&lt;tr class="x-boundlist-item selectorCls"&gt;&lt;td class="x-boundlist-item-td selectorTdCls" width="70%"&gt;64Columns&lt;/td&gt;&lt;td class="x-boundlist-item-td" align="right" style="padding-right:4px;"&gt;&lt;a href="#" style="color:#15428B;"&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/body&gt;&lt;/html&gt; 的 HTML 文件,在 Chrome 的控制台中,我输入 document.body.querySelectorAll('.x-boundlist-item-td.selectorTdCls'); 并返回 [&lt;td class=​"x-boundlist-item-td selectorTdCls" width=​"70%"&gt;​64Columns​&lt;/td&gt;​]
    • 感谢您的快速回复。问题是当我在 chrome 控制台中尝试时,我得到了想要的结果。但是当我在 casperjs 测试中编写相同的代码时,我没有得到想要的结果:(
    猜你喜欢
    • 2015-04-28
    • 2012-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-08
    • 2019-08-21
    • 1970-01-01
    • 2016-01-21
    相关资源
    最近更新 更多