2017-09-25 89 views
-2
 <table class="footable table table-hover table-striped table-bordered" 
    cellspacing="0" cellpadding="6" border="0"> 
    <thead> 
    <tr class="CartBlueHeader"> 
    <th align="10%">PNR No</th> 
    <th width="23%" align="center">Origin</th> 
    <th width="22%" align="center">Destination</th> 
    <th width="10%">Departure</th> 
    <th width="10%">Return</th> 
    <th width="10%">Amount</th> 
    <th width="15%"/> 
    </tr> 
    </thead> 
    <tbody> 
     <tr class="BGLightblue font11"> 
     <td align="left"> Q2S2SO </td> 
     <td align="left"> Dubai Intl Airport </td> 
     <td align="left"> Hindustan Airport </td> 
     <td align="center"> 30 Sep 17 </td> 
     <td align="center">-</td> 
     <td align="left"> 608.00 SAR </td> 
     <td align="left"> 
    </tr> 
    </tbody> 
    </table> 

想要根據表中的標題值檢索值元素。我如何繼續這個任何想法。web表格處理selenium webdriver java

在此先感謝

+0

我的意思是,我需要根據td打印td的值它的價值。 –

+0

發佈你的代碼你試過的是什麼 – zsbappa

+0

創建一個以「行(tr)」元素爲輸入的類,然後將每個「td」映射到它根據標題創建的變量。 –

回答

0

按照模式只

// Grab the table 
WebElement table = driver.findElement(By.id("divListView")); 

// Now get all the TR elements from the table 
List<WebElement> allRows = table.findElements(By.tagName("tr")); 

// And iterate over them, getting the cells 
for (WebElement row : allRows) { 
    List<WebElement> cells = row.findElements(By.tagName("td")); 

    // Print the contents of each cell 
    for (WebElement cell : cells) { 
     System.out.println(cell.getText()); 
    } 
} 

希望它會幫助你

+0

我們可以做些什麼與清單 –

+0

你必須抓住一個循環內的數據 – zsbappa

+0

清單 header = driver.findElements(By.xpath(「.//*[@id ='main-mid-area']/div/DIV /形式/表/ THEAD/TR /日「)); –

0

表類:

public Table { 

    private WebDriver driver; 

    private By baseLocator; 
    private By headCellLocator; 
    private By bodyRowLocator; 
    private By bodyCellLocator; 

    private String[] headColumns; 
    private List<WebElement> findRows; 

    public Tabele(WebDriver, driver, By baseLocator) { 
    this.driver = driver; 
    this.baseLocator = baseLocator; 
    } 

    public Table init(headCellLocator, bodyRowLocator, bodyCellLocator) { 
    this.headCellLocator = headCellLocator; 
    this.bodyRowLocator = bodyRowLocator; 
    this.bodyCellLocator = bodyCellLocator; 

    this.readHeadColumns(); 

    return this; 
    } 

    private readHeadColumns() { 
    List<WebElement> headCells = this.driver 
     .findElement(this.baseLocator) 
     .findElements(this.headCellLocator); 

    for(WebElement headCell:headCells) { 
     ArrayUtils.add(this.headColumns, headCell.getText().trim()) 
    } 
    } 

    public Table findRows() { 
     this.findRows = this.driver 
      .findElement(this.baseLocator) 
      .findElements(this.bodyRowLocator); 

     return this; 
    } 

    public Table findRows(String columnName, String columnValue) { 

    int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName); 

    findRows(); 

    List<WebElement> matchRows = new ArrayList<WebElement>(); 

    for(WebElement row:findRows) { 
      String cellText = row.findElements(this.bodyCellLocator) 
       .get(columnIndex) 
       .getText() 
       .trim(); 

      if(cellText.equals(columnValue)) { 
       matchRows.add(row); 
      } 
    } 

    this.findRows = matchRows; 

    return this; 
    } 

    public String[] readColumnValue(String columnName) { 

    int columnIndex = ArrayUtils.indexOf(this.headColumns, columnName); 
     String[] columnValue; 
     String cellText; 

    for(WebElement row: findRows) { 
     cellText = String cellText = row.findElements(this.bodyCellLocator) 
       .get(columnIndex) 
       .getText() 
       .trim(); 

     ArrayUtils.add(columnValue, cellText); 
    } 

    return columnValue; 
    } 
} 

Table table = new Table(By.cssLocator('body .footable')); 

table.init(By.cssLocator('thead th'), By.cssLocator('tbody > tr'), By.cssLocator('td')); 

table.findRows().readColumnValue('PNR No'); 

// or 
table.findRows('Origin', 'Dubai Intl Airport').readColumnValue('PNR No'); 
相關問題