【问题标题】:Relative Xpath from list of WebElementsWebElements 列表中的相对 Xpath
【发布时间】:2013-01-28 01:34:16
【问题描述】:

在我收集了 WebElements 列表之后,是否可以使用相对 xpath?如果是这样,最好的方法是什么?

这就是我所拥有的:

List <WebElement> rows = driver.findElements(By.xpath(rowsXpath));
int j = 1;
boolean foundCollector = false;
for(WebElement e : rows) {
    String c = e.findElement(By.xpath("(//tbody)[" + j + "]//u[text()=\"" + collector + "\"])")).getText();
    if(c.equals(collector)) {
        foundCollector = true;
        break;
    }
    ++j;
}

列表行包含具有多列的单个行,我试图在列中找到包含特定名称的行。在我的 for 循环中,我试图使用相对 xpath 来查找元素,这甚至可能吗?或者我是否需要提供用于查找行的整个 xpath 以及单个 div 的附加 xpath?

我不能保证列的顺序,这就是我必须这样做的原因。

这是我用来获取行的 xpath:

String rowsXpath = "//div[@id=\"QosDashpardPanelBottom\"]//div[@id=\"CollectorQoSPerformanceMetricsgrid\"]//div[contains(@class, \"x-grid3-row  x-unselectable-single\")]";

这是 HTML:

<body class=" ext-gecko ext-windows" onload="processLoaders();">
<script type="text/javascript">
<div id="topNavDiv" style="height: 90px">
<div id="bodyContentOuter" style="width: 1280px; height: 587px;">
    <div id="bodyContentInner">
        <script src="RLT/RLT.nocache.js" language="javascript" type="text/javascript">
        <script defer="defer">
        <script language="javascript" type="text/javascript">
        <div id="rtcpMain" class=" x-component x-border-layout-ct" style="width: 1280px; height: 587px;">
            <div id="rtcpMainWest" class=" x-panel x-component x-border-panel" style="left: 5px; top: 5px; width: 225px;">
            <div id="x-auto-2" class=" x-tab-panel x-component x-border-panel" tabindex="0" hidefocus="true" style="left: 235px; top: 5px; width: 1040px;">
                <div class="x-tab-panel-header x-unselectable" style="width: 1038px;" unselectable="on">
                <div class="tone-rtcp-tabbed-content-panel" style="width: 1040px; height: 550px;">
                    <div id="x-auto-10" class=" x-component" style="overflow: auto; width: 1040px; height: 550px;">
                        <div id="QosDashboardPanel0" class=" x-panel x-component" style="width: 1040px;">
                            <div id="x-auto-13" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation">
                            <div id="rtcpDPBWrap" class="x-panel-bwrap" role="presentation" style="overflow: visible;">
                                <div class="x-panel-tbar x-panel-tbar-noheader" role="presentation" style="width: 1040px;">
                                <div id="rtcpDPBody" class="x-panel-body x-panel-body-noheader x-border-layout-ct" role="presentation" style="width: 1038px; height: 700px;">
                                    <div id="QosDashboardPanelTop" class=" x-component x-border-panel x-border-layout-ct" style="left: 0px; top: 0px; width: 1038px; height: 400px;">
                                    <div id="QosDashpardPanelBottom" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 401px; width: 1036px; height: 298px;">
                                        <div id="QosDashpardPanel_pqosChartsLC" class=" x-component x-border-panel x-border-layout-ct x-hide-display" style="left: 0px; top: 0px; width: 1036px; height: 400px;">
                                        <div id="QosDashpardPanel_metricsTablesLC" class=" x-component x-border-panel x-border-layout-ct" style="left: 1px; top: 1px; width: 1034px; height: 296px;">
                                            <div id="CollectorMetrics_toneletWrapper" class=" x-panel x-component x-border-panel" style="left: 1px; top: 1px; width: 330px;">
                                                <div id="x-auto-99" class=" x-small-editor x-panel-header x-component x-hide-display" role="presentation">
                                                <div class="x-panel-bwrap" role="presentation">
                                                    <div class="x-panel-body x-panel-body-noheader" role="presentation" style="width: 328px; height: 292px;">
                                                        <div id="CollectorMetrics" class=" x-panel x-component" style="width: 328px;">
                                                            <div id="x-auto-109" class=" x-small-editor x-panel-header x-component x-unselectable" role="presentation" unselectable="on">
                                                            <div class="x-panel-bwrap" role="presentation">
                                                                <div class="x-panel-body" role="presentation" style="width: 326px; height: 265px;">
                                                                    <div id="CollectorQoSPerformanceMetricsgrid" class=" x-grid-panel x-component" style="position: relative; width: 326px; height: 265px;" tabindex="0" hidefocus="true" unselectable="">
                                                                        <div class="x-grid3" role="presentation" style="width: 326px; height: 265px;">
                                                                            <div class="x-grid3-viewport" role="presentation">
                                                                                <div class="x-grid3-header" role="presentation">
                                                                                <div class="x-grid3-scroller" role="presentation" style="width: 326px; height: 243px;">
                                                                                    <div class="x-grid3-body" role="presentation">
                                                                                        <div id="CollectorQoSPerformanceMetricsgrid_x-auto-633" class="x-grid3-row x-unselectable-single x-grid3-row-selected x-grid3-highlightrow " style="width:510px;">
                                                                                            <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation">
                                                                                                <tbody role="presentation">
                                                                                                    <tr role="presentation">
                                                                                                        <td id="x-auto-634" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on">
                                                                                                                <u>Lync</u>
                                                                                                            </div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-635" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">36</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-636" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">120</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-637" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">223</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-638" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.80</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-639" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">29</div>
                                                                                                        </td>
                                                                                                        <td id="x-auto-640" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell">
                                                                                                            <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.86</div>
                                                                                                        </td>
                                                                                                    </tr>
                                                                                                </tbody>
                                                                                            </table>
                                                                                        </div>
                                                                                        <div id="CollectorQoSPerformanceMetricsgrid_x-auto-641" class="x-grid3-row x-unselectable-single " style="width:510px;">
                                                                                            <table class="x-grid3-row-table" cellspacing="0" cellpadding="0" border="0" style="width:510px;" role="presentation">
                                                                                                <tbody role="presentation">
                                                                                                    <tr role="presentation">
                                                                                                    <td id="x-auto-642" class="x-grid3-col x-grid3-cell x-grid3-td-name x-grid-cell-first " style="width:148px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-name" unselectable="on">
                                                                                                            <u>CUCM-Publisher</u>
                                                                                                        </div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-643" class="x-grid3-col x-grid3-cell x-grid3-td-badCalls " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-badCalls" unselectable="on">3</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-644" class="x-grid3-col x-grid3-cell x-grid3-td-totalCalls " style="width:58px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-totalCalls" unselectable="on">52</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-645" class="x-grid3-col x-grid3-cell x-grid3-td-avgLatency " style="width:73px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgLatency" unselectable="on">190</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-646" class="x-grid3-col x-grid3-cell x-grid3-td-avgLoss " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgLoss" unselectable="on">0.79</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-647" class="x-grid3-col x-grid3-cell x-grid3-td-avgJitter " style="width:58px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgJitter" unselectable="on">31</div>
                                                                                                    </td>
                                                                                                    <td id="x-auto-648" class="x-grid3-col x-grid3-cell x-grid3-td-avgMOS x-grid3-cell-last " style="width:53px;" role="gridcell">
                                                                                                        <div class="x-grid3-cell-inner x-grid3-col-avgMOS" unselectable="on">3.98</div>
                                                                                                    </td>
                                                                                                    </tr>
                                                                                                </tbody>
                                                                                            </table>
                                                                                        </div>
                                                                                    </div>                                                                                                          

【问题讨论】:

    标签: java xpath selenium webdriver selenium-webdriver


    【解决方案1】:

    是的,这绝对是可能的。看到这个:

    // Find the outer element.
    WebElement outer = driver.findElement(By.anything("whatever"));
    
    // This doesn't work, it searches the whole page for the XPath expression.
    WebElement inner = outer.findElement(By.xpath("//some/nasty/xpath"));
    
    // This works! It takes the outer element as root.
    WebElement inner = outer.findElement(By.xpath(".//some/nasty/xpath"));
    

    因此,如果我很好地理解了您的需求,您的 XPath 表达式需要如下所示:

    By.xpath("(.//tbody)[" + j + "]//div[text()=\"" + collector + "\"])")
    

    编辑(天哪,这是很多 HTML,实际上只需要一小部分 :)。)

    既然我已经抓住了你所追求的东西,我认为你根本不需要这种方法。试试这个:

    WebElement row = driver.findElement(By.xpath("id('CollectorQoSPerformanceMetricsgrid')//div[contains(@class,'x-grid3-row x-unselectable-single') and .//u[text()='" + collector + "']]"));
    

    只有 XPath 表达式:

    id('CollectorQoSPerformanceMetricsgrid')
        //div[contains(@class,'x-grid3-row x-unselectable-single')
              and .//u[text()='" + collector + "'] ]
    

    拆开:

    FIND ELEM WITH ID = 'CollectorQoSPerformanceMetricsgrid'
    id('CollectorQoSPerformanceMetricsgrid')
        FIND ALL DIV ELEMS THAT CONTAIN 'x-grid3-row x-unselectable-single' IN @CLASS
        //div[contains(@class,'x-grid3-row x-unselectable-single')
              AND HAVE AN <U> DESCENDANT WITH TEXT EQUAL TO collector VARIABLE
              and .//u[text()='" + collector + "'] ]
    

    这只会为您找到使用单个命令所需的单行。

    如果您想坚持当前的解决方案,请尝试:

    String rowsXpath = "id('CollectorQoSPerformanceMetricsgrid')//div[contains(@class, 'x-grid3-row x-unselectable-single')]";
    List<WebElement> rows = driver.findElements(By.xpath(rowsXpath));
    boolean foundCollector = false;
    for(WebElement row : rows) {
        if(!row.findElements(By.xpath(".//u[text()='" + collector + "']")).isEmpty()) {
            foundCollector = true;
            break;
        }
    }
    

    【讨论】:

    • 我试过了,但它不起作用,我没有得到任何结果。我想我在访问单个行时遇到了问题。例如,如果我在 firebug 上尝试“//nasty/xpath/(.//tbody)[1]”,则完整的 xpath 我没有得到任何回报。
    • @alexxp03 我每天都在使用它,所以这个构造绝对有效。您只需要想出正确的 Xpath。如果可以,请在此处发布您的 HTML 代码,我们会解决的!
    • 我很高兴,这让我发疯了。 html 很大,获取并发布它的最佳方式是什么?
    • @alexxp03 将其复制到记事本中,删除似乎与您的案例无关的所有内容。然后只需将其编辑到您的问题中。如果它仍然太大,请使用 pastebin.com 或其他东西。我半小时后回来。如果需要,您也可以为此创建一个聊天室并在此处发布链接。
    • 0k 我发布了 html,虽然格式很糟糕,但我不确定它会有多大帮助。
    【解决方案2】:

    我在这个 xpath 中没有找到行(tr 标记),那么它将如何获得行数。

    String rowsXpath = "//div[@id=\"QosDashpardPanelBottom\"]//div[@id=\"CollectorQoSPerformanceMetricsgrid\"]//div[contains(@class, \"x-grid3-row  x-unselectable-single\")]";
    

    您需要构建类似下面的 xpath 来获取特定表中的行数

    int rowCount=driver.findElements(By.xpath("//table[@class='x-grid3-row-table']/tbody/tr"))
    

    或者,如果您想查找该表中具有特定名称的所有列

    如下构造xpath

    int columnCount=driver.findElements(By.xpath("//table[@class='x-grid3-row-table']/tbody//tr//td[@name='RequiredName']"))
    

    【讨论】:

    • 我的问题是我是否可以在找到一个整体后使用相对路径。假设您的 rowCount xpath 是正确的,我将如何访问由 rowCount 重印的行中的各个列值?
    猜你喜欢
    • 2019-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多