我在使用Xpath定位元素时遇到错误

时间:2018-10-04 05:07:24

标签: selenium selenium-webdriver xpath webdriver protractor

具有以下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');

5 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

要找到文本为 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();
    

答案 2 :(得分:0)

我同意@Faisal Maqbool的回答。我将xpath缩短如下,

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

如果可以的话,您想直接选择确切的元素,只有在找不到其他选项的情况下,才使xpath复杂化。

答案 3 :(得分:0)

在tr [1] td [3]上显示文本为'000079'的元素。您可以使用以下解决方案:

 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);
    }

答案 4 :(得分:0)

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();
                }
            }