如何在 Selenium webdriver 中选择所有列的第一个单元格 (tds)? [英] How do I select all the columns first cells (tds) in Selenium webdriver?
问题描述
我使用的是 Selenium 2.12.使用 WebDriver API,假设我有一个表示表 ( 为了更好地理解表格结构,如果我从 tableElement WebElement 获取 HTML ... 我得到了下面的混乱.要注意的关键是有 6 个 tr 和 16 个 td ... 遗憾的是,这两个表达式都没有产生正确的结果. 这完全符合 XPath W3c 规范,并且是 XPath 中最常见的常见问题之一. 因此, 如果要选择 XML 文档中的第一个 在这种情况下,您只需要给定元素的第一个 使用: 如果需要选择当前节点的所有 甚至更好: I'm using Selenium 2.12. Using the WebDriver API, let's say I have a WebElement that represents a table ( To better understand the table structure, if I get the HTML from my tableElement WebElement ... I get the jumble below. The key thing to notice is that there are 6 tr's and 16 td's ... Sadly, both these expressions do not yield the correct results.
This is exactly as per thw XPath W3c Spec and is one of the most FAQ in XPath. The precedence (priority) of the Therefore, If you want to select the 1st In this case you only want the first Use: If you need all And even better:
这篇关于如何在 Selenium webdriver 中选择所有列的第一个单元格 (tds)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!) 的 WebElement.使用该元素,如何选择第一列中的所有 td?我假设这里有一个 xpath 表达式.
String html = (String)((JavascriptExecutor)driver).executeScript("返回参数[0].innerHTML;", tableElt);
//这将返回零 td最终列表
//这将返回 238 个 td(我认为这是我文档中的所有内容)最终列表
//
伪运算符的优先级(优先级)小于 []
运算符的优先级.//SomeName[1]
选择所有名为 SomeName
的元素,它们是其父元素的第一个 SomeName
子元素——然后可能有很多这样的元素.SomeName
元素,则需要使用方括号显式覆盖默认运算符优先级:(//SomeName)[1]
td
后代 - 因此除了上述更正之外,您还必须将表达式更正为 相对 -——不是绝对的.一个绝对的 XPath 表达式(以 /
开始总是被评估为文档节点作为附加上下文节点).(.//td)[1]
td
后代,则使用:.//td
.//后代::td
<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.String html = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", tableElt);
<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>
// 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]"));
//
pseudo-operator is less than that of the []
operator.//SomeName[1]
selects all elements named SomeName
that are the first SomeName
child of their parent -- and there may be many such elements.SomeName
element in the XML document, you need to explicitly override the default operator precedence using brackets:(//SomeName)[1]
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).(.//td)[1]
td
descendants of the current node selected, then use:.//td
.//descendant::td
登录
关闭