如何从Selenium Web Driver JAVA中的表中获取元素

时间:2016-07-15 08:42:20

标签: java selenium-webdriver testng testng-eclipse

我正在尝试识别页面的元素。我有桌子,有很多行有多个输入。如何从表行访问输入?

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

}

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:0)

试试这个: 您可以尝试使用xpath而不是id来查找特定的输入元素。如果id可用于特定元素,则可以使用id本身。同样在您的构造函数中,尝试从

更改您的代码
SELECT Pictures 
FROM 
    (SELECT TOP 24 CAST([Pictures] AS varbinary(Max)) AS Pictures 
     FROM Admin_Pic_Lib ORDER BY NEWID()) AS X

 UNION ALL 

  SELECT  CAST([Pictures] AS varbinary(Max)) AS Pictures 
  FROM User_images
  WHERE UserName ='ahmed'

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

并在课堂上尝试添加

public Setting(WebDriver driver)
{
  this.driver = driver;
    /**Initialize PageFactory WebElements*/
    PageFactory.initElements(driver, this);
}   
在你的@FindBy声明之前