【问题标题】:Xpath Selection of preceding-sibling check box in Div of generated tableXpath 在生成表的 Div 中选择前一个兄弟复选框
【发布时间】:2017-03-30 13:14:48
【问题描述】:

我在使用 xpath 选择时遇到了一些问题。

我目前正在使用 C# 和 Selenium

我正在尝试解决一个问题,即我需要在生成的表中选择一个复选框,同时只知道前一个兄弟 Div 中的一行文本。所有 ID 都可以更改,因为它们是随机生成的。

我想选择 class="jqx-checkbox-check-checked" 而只知道文本“testdata”

    <div role="row" style="position: relative; height:28px;" id="row1jqxgrid">
    <div role="gridcell" style="left: 0px; z-index: 790; width:30px;" class="jqx-grid-cell jqx-item jqx-grid-cell-alt jqx-grid-cell-selected jqx-fill-state-pressed">
        <div style="position: absolute; top: 50%; left: 50%; margin-top: -7px; margin-left: -10px; overflow: visible; cursor: auto;" id="jqxWidget3af6ac8d" tabindex="0" class="jqx-widget jqx-checkbox">
            <div class="jqx-checkbox-default jqx-fill-state-normal jqx-rc-all">
                <div style="width: 13px; height: 13px;"><span style="width: 13px; height: 13px;" class="jqx-checkbox-check-checked"></span></div>
            </div>
            <div style="clear: both;"></div>
        </div>
    </div>
    <div role="gridcell" style="left: 30px; z-index: 789; width:25px;display: none;" class="jqx-grid-cell"></div>
    <div role="gridcell" style="left: 30px; z-index: 789; width:200px;" class="jqx-grid-cell jqx-item jqx-grid-cell-alt jqx-grid-cell-selected jqx-fill-state-pressed">
        <div class="jqx-grid-cell-left-align" style="margin-top: 6px;"><a id="id_01" href="/test/testing?id_01=199">testdata</a></div>
    </div>
</div>

【问题讨论】:

  • 试试//div[div[div[text()="testdata"]]]//div/span

标签: c# selenium xpath


【解决方案1】:

您可以尝试选择包含字符串testdata 的任意位置的row,并在该row 中选择具有所需类的div

第一次尝试是:

//div[@class='row'][.//div[text()='testdata']]//span[@class='jqx-checkbox-check-checked']

这是最简单的方法。

【讨论】:

    【解决方案2】:

    您可以使用testdata 选择元素,转到祖先 div,然后转到您需要的跨度:

    //*[text()='testdata']/ancestor::div[@role='row']//span
    

    你也可以指定你需要的span的类或部分类:

    //*[text()='testdata']/ancestor::div[@role='row']//span[contains(@class,'checked')]
    
    //*[text()='testdata']/ancestor::div[@role='row']//span[@class = 'jqx-checkbox-check-checked']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2012-08-02
      • 2012-04-17
      • 2016-04-02
      • 2014-02-09
      • 1970-01-01
      相关资源
      最近更新 更多