如何用硒替换excel中的特定单词?

时间:2018-04-18 06:34:51

标签: selenium-webdriver

我想通过使用selenium删除excel中的id no(Text),如下图所示:

enter image description here

我该怎么办?

从网站上获取Name和Id No之类的值,但它只有一个由div标签分隔的td标签,因为在excel中它会在一个单元格中打印出name和id,但是我只想在excel中打印Name片

这是我的HTML代码:

enter code here <table>
                <tr>
                <td colspan="100">
                <div>Name</div>
                <div class="clearfix scheme-name">
                <a id="name-2019-7993063096" class="" target="new">
                Alice Lopez
                </a>
                </div>
                <div class="clearfix header-resp">
                Id no
                </div>
                <div id="sch-inv-2019-7993063096" class="clearfix scheme-mtm" data-v-min="-999999999.99" data-d-group="2">
                 AS4567H
                 </div>
                 </td>
                 </tr> 
                 <tr>
                 <td colspan="100">
                 <div>Name</div>
                 <div class="clearfix scheme-name">
                 <a id="name-2019-7993063096" class="" target="new">
                 John Stark
                 </a>
                 </div>
                 <div class="clearfix header-resp">
                 Id no
                 </div>
                <div id="sch-inv-2019-7993063096" class="clearfix scheme-mtm" data-v-min="-999999999.99" data-d-group="2">
                 B8765M
                 </div>
                 </td>
                 </tr>
                 </table>

这是我的代码:

WebElement Table = driver.findElement(By.xpath(".//*[@id='schemeGridHolder']/div/div[6]/div/table"));
        List<WebElement> rows = Table.findElements(By.tagName("tr"));
        Iterator<WebElement> i = rows.iterator();
        int x=0;
        while(i.hasNext())
        {
            WebElement row = i.next();
            List<WebElement> columns = row.findElements(By.tagName("td"));
            Iterator<WebElement> j = columns.iterator();
            int y=1;
            while(j.hasNext())
            {
                WebElement column = j.next();
                System.out.println(column.getText());
                Row dataRow = sheet23.createRow((short)x);
                System.out.println("\n");

                String str2 = "Id No";
                String str3=" ";

                if(column.getText().contains(str2))
                {
                str2=column.getText().replace(str2, str3);
                //str2=column.getText();
                }

                Cell cell12 = dataRow.createCell(y);
                cell12.setCellType(Cell.CELL_TYPE_STRING);
                cell12.setCellValue(column.getText());
                y=y+1;

            }
            x=x+1;
        }

0 个答案:

没有答案