我在使用 Xpath 定位元素时出错



具有以下html。 我想使用 xpath 选择一个特定的元素

<tbody>
<tr role="row" id="gridview-1034-record-ext-record-1" data-boundview="gridview-1034" data-recordid="ext-record-1" data-recordindex="0" class="x-grid-row x-grid-data-row x-grid-row-selected x-grid-row-focused" tabindex="-1">
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1010 x-grid-cell-first x-unselectable " id="ext-gen1139">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">03 Oct 2018</div>
</td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1013 x-unselectable " id="ext-gen1140">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">Sales Quotation</div>
</td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1016 x-unselectable " id="ext-gen1141">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">000079</div>
</td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1019 x-unselectable " id="ext-gen1142">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">ABC Bearings Ltd.</div>
</td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1022 x-unselectable " id="ext-gen1143">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">&nbsp;</div></td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1025 x-unselectable " id="ext-gen1144"><div unselectable="on" class="x-grid-cell-inner " style="text-align:right;">8,000.00</div>
</td><td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1028 x-unselectable " id="ext-gen1145">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">No</div></td>
<td role="gridcell" class="x-grid-cell x-grid-td x-grid-cell-gridcolumn-1031 x-grid-cell-last x-unselectable " id="ext-gen1146">
<div unselectable="on" class="x-grid-cell-inner " style="text-align:left;">Approved</div>
</td>
</tr>
</tbody>

我无法找到下面的元素,并使用以下代码使用 XPath 单击它。

帮助将不胜感激

element(by.xpath('//tbody/tr/td/div')).click();
expect(div.getText()).toBe('000079');

您可以使用contains(text(),'...')来匹配所需的div,然后单击它。

element = driver.findElement(By.xpath("//tbody/tr/td/div[contains(text(), 'div text you want')]"));
element.click()

我同意@Faisal Maqbool的回答。我会按如下方式缩短 xpath,

element = driver.findElement(By.xpath("//div[contains(text(), 'div text you want')]"));element.click();

如果可以的话,您想尝试直接选择确切的元素,仅当找不到其他选项时,才使 xpath 复杂化。

若要查找带有文本的元素000079可以使用以下解决方案:

  • XPath

    //td[starts-with(@id,'ext-gen')]/div[@class='x-grid-cell-inner' and contains(.,'000079')]
    
  • 代码行:

    element(by.xpath("//td[starts-with(@id,'ext-gen')]/div[@class='x-grid-cell-inner' and contains(.,'000079')]")).click();
    

带有文本"000079"的元素存在于tr[1]td[3]中。您可以使用以下解决方案:

WebElement e1= driver.findElement(By.xpath("//tbody/tr[1]/td[3]/div[1]"));
String Str=e1..getText();
if(Str="000079")
{ System.out.println("Text matched"+Str);
e1.click();
}
else
{
System.out.println("Text Mismatched"+Str);
}
String Str = "000079";
List<WebElement> listEle = driver.findElements(By.Xpath("Your Xpath"));
System.out.println("List Printer " + listEle);
Iterator<WebElement> iter = listEle.iterator();
System.out.println("Iterator Print" + iter);
while (iter.hasNext()) {
WebElement we = iter.next();
System.out.println("Web Element We " + we.getText());
if (we.getText().equals(str)) {
we.click();
}
}

最新更新