web-dev-qa-db-fra.com

Comment obtenir du texte de chaque cellule d'un tableau HTML?

Dans Selenium 2.0, je ne sais pas comment parcourir un tableau HTML dans une page Web. Dans Selenium 2.0 javadoc, j'ai trouvé deux classes "TableFinder" et "TableCellFinder", mais je n'ai trouvé aucun exemple.

Je veux faire quelque chose comme ça:

RowCount=Get how many rows are there in the html table

for each row of the table
{
   column_count=Get column count
   for each column
   {
      cell_value=get_text_from(row,col);
      Do something with cell_value
   }
}

Comment puis-je obtenir le texte de chacune des cellules du tableau?

25
Vikas

Merci pour la réponse précédente.

J'ai compris les solutions en utilisant les classes Selenium 2.0.

import Java.util.List;

import org.openqa.Selenium.By;
import org.openqa.Selenium.WebDriver;
import org.openqa.Selenium.WebElement;
import org.openqa.Selenium.ie.InternetExplorerDriver;

public class WebTableExample 
{
    public static void main(String[] args) 
    {
        WebDriver driver = new InternetExplorerDriver();
        driver.get("http://localhost/test/test.html");      

        WebElement table_element = driver.findElement(By.id("testTable"));
        List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr"));

        System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size());
        int row_num,col_num;
        row_num=1;
        for(WebElement trElement : tr_collection)
        {
            List<WebElement> td_collection=trElement.findElements(By.xpath("td"));
            System.out.println("NUMBER OF COLUMNS="+td_collection.size());
            col_num=1;
            for(WebElement tdElement : td_collection)
            {
                System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText());
                col_num++;
            }
            row_num++;
        } 
    }
}
46
Vikas

Voici un exemple en C # que je viens de vous concocter, basé vaguement sur la réponse à l'aide de sélecteurs CSS, que nous espérons utile à d'autres utilisateurs pour voir comment configurer une ReadOnlyCollection de lignes de table et la parcourir au moins dans les pays MS. Je regarde une collection de lignes de table pour trouver une ligne avec une OriginatorsRef (juste une chaîne) et une TD avec une image qui contient un attribut title avec Overdue by dedans:

    public ReadOnlyCollection<IWebElement> GetTableRows()
    {
        this.iwebElement = GetElement();
        return this.iwebElement.FindElements(By.CssSelector("tbody tr"));
    }

Et dans mon code principal: 

        ...
        ReadOnlyCollection<IWebElement> TableRows;
        TableRows = f.Grid_Fault.GetTableRows();

        foreach (IWebElement row in TableRows)
        {
            if (row.Text.Contains(CustomTestContext.Current.OriginatorsRef) &&
              row.FindElements(By.CssSelector("td img[title*='Overdue by']")).Count > 0)
                return true;
        }
8
Christh
$content = '';
    for($rowth=0; $rowth<=100; $rowth++){
        $content .= $Selenium->getTable("tblReports.{$rowth}.0") . "\n";
        //$content .= $Selenium->getTable("tblReports.{$rowth}.1") . "\n";
        $content .= $Selenium->getTable("tblReports.{$rowth}.2") . " ";
        $content .= $Selenium->getTable("tblReports.{$rowth}.3") . " ";
        $content .= $Selenium->getTable("tblReports.{$rowth}.4") . " ";
        $content .= $Selenium->getTable("tblReports.{$rowth}.5") . " ";
        $content .= $Selenium->getTable("tblReports.{$rowth}.6") . "\n";

    }
0
nguyen thanh huy

Un autre exemple en C #. Je viens de faire une méthode d'extension pour cela.

public static string GetCellFromTable(this IWebElement table, int rowIndex, int columnIndex)
    {
        return table.FindElements(By.XPath("./tbody/tr"))[rowIndex].FindElements(By.XPath("./td"))[columnIndex].Text;
    }
0
Drew