在我收集了一个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;
}
list rows包含具有多列的单个行,我正在尝试找到在列中包含特定名称的行。在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>
是的,这肯定是有可能的。请看下面的内容:
// 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;
}
}
我试图自动化以下场景: < li >访问amazon.com < li >搜索耳机 < li >将第一个结果页面中的所有畅销书添加到购物车中 我为编写此场景所遵循的步骤: < li >访问amazon.com < li >在搜索字段中输入文本“耳机” < li >单击搜索按钮 < li >单击标记为“畅销书”的链接 < li >单击“添加到购物车”按钮 < li >导航回结果页面 < li >单击
WebElements 是一组 Python 对象用来在服务器端生成 Web 应用,面向服务器端开发,代码可非常方便的进行重用。 示例代码: from WebElements import Document, Displaypage = Document()page += Display.Label(text="Hello World!")print page生成的代码: <!DOCTYPE ht
问题内容: 我想遍历一个webelements列表并从每个元素中返回文本,但是我只从第一个元素而不是从其他标记内的其余元素中获取文本,然后存在存在该循环的代码 这是我要从中提取文本的HTML代码的一部分: 这是Pyhton代码: 问题答案: 一直在我的脸上,这将打印每个元素的文本,很高兴我能发现
本文向大家介绍在Python中查找列表中元素的相对顺序,包括了在Python中查找列表中元素的相对顺序的使用技巧和注意事项,需要的朋友参考一下 我们给了一个列表,其元素是整数。我们需要找到相对顺序,这意味着如果它们以升序排序,那么我们需要找到它们位置的索引。 带排序和索引 我们首先对整个列表进行排序,然后在排序之后找出每个列表的索引。 示例 输出结果 运行上面的代码给我们以下结果- 用枚举和排序
问题内容: 这是对该问题的更多甚至更好的描述: 我做一个简单的选择查询。返回列表包含确切的记录/对象数,就好像我在数据库中进行相同的查询一样,但是问题在于所有对象都是相同/相同的。 例如,这是数据库中的结果(我删除了空值): 26801 01-JAN-00 7 31-DEC-99 7 Obere Kirchstr。26 CH 8304 Walliselln 26801 01-JAN-00 2 31
我按直接顺序列出了列表1<代码>列表 如何更改顺序。我不知道如何重写扩展类中的方法,请用例子写或说清楚。