无法使用xpath和java脚本执行程序从表中获取文本

时间:2016-03-23 06:10:22

标签: java selenium xpath selenium-webdriver

想要获得tab div下的文本48630和49928以及每个tr的第一个td中的文本。 只需要一个xpath来获取div中的文本或表中存在相应的数据。

     <table class="x-grid-table x-grid-table-resizer" cellspacing="0"     cellpadding="0" border="0" style="width: 1505px;">
    <tbody>
    <tr class="x-grid-header-row">
    <tr class="x-grid-row">
    <td id="ext-gen1289" class=" x-grid-cell x-grid-cell-gridcolumn-1126 x-grid-cell-first">
       <div id="ext-gen1290" class="x-grid-cell-inner x-unselectable"     style="background-color: #BECFE1;; text-align: left;"     unselectable="on">48630</div>
    </td>
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1127 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1128 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1129 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1130 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1131 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1132 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1133 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1134 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1135 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1136 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1137 x-grid-cell-last">
    </tr>
    <tr class="x-grid-row x-grid-row-alt">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1126 x-grid-cell-first">
        <div class="x-grid-cell-inner x-unselectable" style="background-color:#EAEEF5;; text-align: left;" unselectable="on">
            <a        onclick="Travel.common.Common.viewport.items.getAt(1).OManagePanel.loadSubscript    ion()" href="#">49928</a>
    </div>
    </td>
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1127 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1128 ">
    <td class=" x-grid-cell x-grid-cell-gridcolumn-1129 ">

尝试了以下xpaths选项 -

WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/(div|div/a)")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/div | //table/tbody//tr["+i+"]/td/div/a")); 
WebElement element = driver.findElement(By.xpath("(//table/tbody//tr["+i+"]/td/div) | (//table/tbody//tr["+i+"]/td/div/a)"));
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/child::node()[contains(text(), *)]")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td[1]/descendant::*[contains(text(), *)]")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td[1]/descendant::node()[contains(text(), *)]")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/(div|div/a)")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/div | //table/tbody//tr["+i+"]/td/div/a")); 
WebElement element = driver.findElement(By.xpath("(//table/tbody//tr["+i+"]/td/div) | (//table/tbody//tr["+i+"]/td/div/a)"));
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td/child::node()[contains(text(), *)]")); 
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td[1]/descendant::*[contains(text(), *)]"));
WebElement element = driver.findElement(By.xpath("//table/tbody//tr["+i+"]/td[1]/descendant::node()[contains(text(), *)]")); 


text = (String) js.executeScript("return arguments[0].text", element);
            allSetSubscriptions2.add(text);

3 个答案:

答案 0 :(得分:0)

你为什么不尝试这样的事情:

//div[@id='ext-gen1290']/text()

或者如果id是动态的。您可以使用:

//td[contains(@class,'x-grid-cell x-grid-cell-gridcolumn-')]//a/text()

答案 1 :(得分:0)

试试这个:

//div[@class="x-grid-cell-inner x-unselectable"]/a');

答案 2 :(得分:0)

xpath: //div[@unselectable="on"]

您可以使用上面的xpath并查找与unselectable属性匹配的元素,并且可以迭代并将文本存储在数组中。

类似的东西,

List<String> text_item= new ArrayList<String>();
allElements= driver.findElements(By.xpath('//div[@unselectable="on"]'))
for (WebElement element: allElements) {
      text_item.add(allElements.getText())
}
你应该[48630,49928]