2016-07-15 30 views
0

我正在嘗試識別頁面的元素。我有桌子,有很多行與多個輸入。如何訪問表格行的輸入?如何從Selenium Web驅動程序中的表中獲取元素JAVA

This is my java class 

public class Setting extends Page { 

    /**Identify Elements of the page*/ 

    @FindBy(id="table-settings") 

    private WebElement Table; 

    //Constructor 
    public Setting() 
    { 
     /**Initialize PageFactory WebElements*/ 
     PageFactory.initElements(driver, this); 
    } 

} 

回答

0

請參閱下面的代碼,做出相應的調整,讓你找到它

@Test 
public void test() { 
    WebElement Webtable=driver.findElement(By.id("TableID")); // Replace TableID with Actual Table ID or Xpath 

    List<WebElement> TotalRowCount=Webtable.findElements(By.xpath("//*[@id='TableID']/tbody/tr")); 

    System.out.println("No. of Rows in the WebTable: "+TotalRowCount.size()); 
    // Now we will Iterate the Table and print the Values 

    int RowIndex=1; 

    for(WebElement rowElement:TotalRowCount) 
    { 
     List<WebElement> TotalColumnCount=rowElement.findElements(By.xpath("td")); 
     int ColumnIndex=1; 

     for(WebElement colElement:TotalColumnCount) 
     { 
      System.out.println("Row "+RowIndex+" Column "+ColumnIndex+" Data "+colElement.getText()); 
      ColumnIndex=ColumnIndex+1; 
     } 
     RowIndex=RowIndex+1; 
    } 
} 
+0

如果有效,那麼你可以評價問題 –

0

試試這個任何障礙我知道: 您可以使用XPath,而不是ID查找特定的input元素嘗試。如果id可用於特定元素,則可以使用id本身。另外,在你的構造函數,嘗試從

public Setting() 
{ 
    /**Initialize PageFactory WebElements*/ 
    PageFactory.initElements(driver, this); 
} 

改變你的代碼,以

public Setting(WebDriver driver) 
{ 
    this.driver = driver; 
    /**Initialize PageFactory WebElements*/ 
    PageFactory.initElements(driver, this); 
} 

,並在您嘗試類的@FindBy語句前加上

WebDriver driver; 

相關問題