如何在 Selenium webdriver 中选择所有列的第一个单元格 (tds)? [英] How do I select all the columns first cells (tds) in Selenium webdriver?

查看:22
本文介绍了如何在 Selenium webdriver 中选择所有列的第一个单元格 (tds)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Selenium 2.12.使用 WebDriver API,假设我有一个表示表 (

) 的 WebElement.使用该元素,如何选择第一列中的所有 td?我假设这里有一个 xpath 表达式.

为了更好地理解表格结构,如果我从 tableElement WebElement 获取 HTML ...

String html = (String)((JavascriptExecutor)driver).executeScript("返回参数[0].innerHTML;", tableElt);

我得到了下面的混乱.要注意的关键是有 6 个 tr 和 16 个 td ...

DSSUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">10 </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;"></div></td></tr><tr onclick="" class="GCSPOWVAF"><td class="GCSPOWVPD GCSPOWVBF GCSPOWVCE"><div style="outline:none;">ETTUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;">30 </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBF GCSPOWVME"><div style="outline:none;"></div></td></tr><tr onclick="" class="GCSPOWVAE"><td class="GCSPOWVPD GCSPOWVBE GCSPOWVCE"><div style="outline:none;">FCCUAQR6IE6E </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">20 </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;"></div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;"></div></td></tr></tbody><tbody style="display: none;"><tr><td colspan="5" align="center"><div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;"><div style="width: 100%; height:100%; display: none;"></div></div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;">;<div class="GCSPOWVPE" style="width: 100%; height: 100%; display: none;"><img class="gwt-Image" onload='this.__gwtLastUnhandledEvent="load";'src="http://localhost:9080/cme-productplus-web/productplus/clear.cache.gif" style="width: 43px; height: 11px; background: url(data:image/gif;base64,R0lGODlhKwALAPEAAP///0tKSqampktKSiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh + QQJCgAAACwAAAAAKwALAAACMoSOCMuW2diD88UKG95W88uF4DaGWFmhZid93pq + pwxnLUnXh8ou + SSZ + T64oCAyTBUAACH5BAkKAAAALAAAAAArAAsAAAI9xI4IyyAPYWOxmoTHrHzzmGHe94xkmJifyqFKQ0pwLLgHa82xrekkDrIBZRQab1jyfY7KTtPimixiUsevAAAh + QQJCgAAACwAAAAAKwALAAACPYSOCMswD2FjqZpqW9xv4g8KE7d54XmMpNSgqLoOpgvC60xjNonnyc7p + VKamKw1zDCMR8rp8pksYlKorgAAIfkECQoAAAAsAAAAACsACwAAAkCEjgjLltnYmJS6Bxt + sfq5ZUyoNJ9HHlEqdCfFrqn7DrE2m7Wdj/2y45FkQ13t5itKdshFExC8YCLOEBX6AhQAADsAAAAAAAAAAAA =)不重复0像素0像素;"border="0"></div></div></div></td></tr></tbody><tfoot style="display: none;"><tr><th colspan="5" class="GCSPOWVFE GCSPOWVDE GCSPOWVNE"></th></tr></tfoot>

遗憾的是,这两个表达式都没有产生正确的结果.

//这将返回零 td最终列表tds = tableElt.findElements(By.xpath("/tr/td[1]"));...//这将返回 238 td(我认为这是我文档中的所有内容)最终列表tds = tableElt.findElements(By.xpath("//td[1]"));

解决方案

//这将返回 238 个 td(我认为这是我文档中的所有内容)最终列表tds = tableElt.findElements(By.xpath("//td[1]"));

这完全符合 XPath W3c 规范,并且是 XPath 中最常见的常见问题之一.

// 伪运算符的优先级(优先级)小于 [] 运算符的优先级.

因此,//SomeName[1] 选择所有名为 SomeName 的元素,它们是其父元素的第一个 SomeName 子元素——然后可能有很多这样的元素.

如果要选择 XML 文档中的第一个 SomeName 元素,则需要使用方括号显式覆盖默认运算符优先级:

(//SomeName)[1]

在这种情况下,您只需要给定元素的第一个 td 后代 - 因此除了上述更正之外,您还必须将表达式更正为 相对 -——不是绝对的.一个绝对的 XPath 表达式(以 / 开始总是被评估为文档节点作为附加上下文节点).

使用:

(.//td)[1]

如果需要选择当前节点的所有td后代,则使用:

.//td

甚至更好:

.//后代::td

I'm using Selenium 2.12. Using the WebDriver API, let's say I have a WebElement that represents a table (<table>). Using that element, how do I select all the td's in the first column? I'm assuming an xpath expression is in order here.

To better understand the table structure, if I get the HTML from my tableElement WebElement ...

String html = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", tableElt); 

I get the jumble below. The key thing to notice is that there are 6 tr's and 16 td's ...

<thead><tr><th colspan="1" class="GCSPOWVGE GCSPOWVEE GCSPOWVEF GCSPOWVFF"><div style="padding-left: 17px;position:relative;zoom:1;"><div style="left:0px;margin-top:-4px;position:absolute;top:50%;line-height:0px;"><img onload='this.__gwtLastUnhandledEvent="load";' src="http://localhost:9080/cme-productplus-web/productplus/clear.cache.gif" style="width: 11px; height: 7px; background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAAHCAYAAADebrddAAAAiklEQVR42mNgwALyKrumFRf3iDAQAvmVXVVAxf/zKjq341WYV95hk1fZ+R+MK8C4HqtCkLW5FZ2PQYpyK6AaKjv/5VV1OmIozq3s3AFR0AXFUNMrO5/lV7WKI6yv6mxCksSGDyTU13Mw5JV2qeaWd54FWn0BRAMlLgPZl/NAuBKMz+dWdF0H2hwCAPwcZIjfOFLHAAAAAElFTkSuQmCC) no-repeat 0px 0px;" border="0"></div><div>GUID</div></div></th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung Ratio</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung type</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF">Fung Date Offset</th><th colspan="1" class="GCSPOWVGE GCSPOWVEF  GCSPOWVOE">Days To Retain</th></tr></thead><colgroup><col><col><col><col><col></colgroup><tbody><tr onclick="" class="GCSPOWVAE"><td class="GCSPOWVPD GCSPOWVBE GCSPOWVCE"><div style="outline:none;" tabindex="0">      DSSUAQR6IE6E    </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">      10      </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">              </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">      </div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;">            </div></td></tr><tr onclick="" class="GCSPOWVAF"><td class="GCSPOWVPD GCSPOWVBF GCSPOWVCE"><div style="outline:none;">      ETTUAQR6IE6E    </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;">      30      </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;">              </div></td><td class="GCSPOWVPD GCSPOWVBF"><div style="outline:none;">      </div></td><td class="GCSPOWVPD GCSPOWVBF GCSPOWVME"><div style="outline:none;">            </div></td></tr><tr onclick="" class="GCSPOWVAE"><td class="GCSPOWVPD GCSPOWVBE GCSPOWVCE"><div style="outline:none;">      FCCUAQR6IE6E    </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">      20      </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">              </div></td><td class="GCSPOWVPD GCSPOWVBE"><div style="outline:none;">      </div></td><td class="GCSPOWVPD GCSPOWVBE GCSPOWVME"><div style="outline:none;">            </div></td></tr></tbody><tbody style="display: none;"><tr><td colspan="5" align="center"><div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;"><div style="width: 100%; height: 100%; display: none;"></div></div><div style="width: 100%; height: 100%; padding: 0px; margin: 0px; display: none;"><div class="GCSPOWVPE" style="width: 100%; height: 100%; display: none;"><img class="gwt-Image" onload='this.__gwtLastUnhandledEvent="load";' src="http://localhost:9080/cme-productplus-web/productplus/clear.cache.gif" style="width: 43px; height: 11px; background: url(data:image/gif;base64,R0lGODlhKwALAPEAAP///0tKSqampktKSiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAKwALAAACMoSOCMuW2diD88UKG95W88uF4DaGWFmhZid93pq+pwxnLUnXh8ou+sSz+T64oCAyTBUAACH5BAkKAAAALAAAAAArAAsAAAI9xI4IyyAPYWOxmoTHrHzzmGHe94xkmJifyqFKQ0pwLLgHa82xrekkDrIBZRQab1jyfY7KTtPimixiUsevAAAh+QQJCgAAACwAAAAAKwALAAACPYSOCMswD2FjqZpqW9xv4g8KE7d54XmMpNSgqLoOpgvC60xjNonnyc7p+VKamKw1zDCMR8rp8pksYlKorgAAIfkECQoAAAAsAAAAACsACwAAAkCEjgjLltnYmJS6Bxt+sfq5ZUyoNJ9HHlEqdCfFrqn7DrE2m7Wdj/2y45FkQ13t5itKdshFExC8YCLOEBX6AhQAADsAAAAAAAAAAAA=) no-repeat 0px 0px;" border="0"></div></div></div></td></tr></tbody><tfoot style="display: none;"><tr><th colspan="5" class="GCSPOWVFE GCSPOWVDE  GCSPOWVNE"></th></tr></tfoot>

Sadly, both these expressions do not yield the correct results.

        // This returns zero td's
        final List<WebElement> tds = tableElt.findElements(By.xpath("/tr/td[1]"));

        ...

        // This returns 238 td's (I think that's everything in my document)
        final List<WebElement> tds = tableElt.findElements(By.xpath("//td[1]"));

解决方案

    // This returns 238 td's (I think that's everything in my document)               
    final List<WebElement> tds = tableElt.findElements(By.xpath("//td[1]"));

This is exactly as per thw XPath W3c Spec and is one of the most FAQ in XPath.

The precedence (priority) of the // pseudo-operator is less than that of the [] operator.

Therefore, //SomeName[1] selects all elements named SomeName that are the first SomeName child of their parent -- and there may be many such elements.

If you want to select the 1st SomeName element in the XML document, you need to explicitly override the default operator precedence using brackets:

(//SomeName)[1]

In this case you only want the first td descendant of a given element -- therefore in addition to the above correction, you must correct your expression to be relative -- not absolute. An absolute XPath expression (starting with / is always evaluated having as aditional context node the document node).

Use:

(.//td)[1]

If you need all td descendants of the current node selected, then use:

.//td

And even better:

.//descendant::td

这篇关于如何在 Selenium webdriver 中选择所有列的第一个单元格 (tds)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆