Tamim
Tamim

Reputation: 73

how to get the table data in c# selenium and save it any doc

driver.SwitchTo().Frame("menu");
WebDriverWait wait1 = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
wait1.Until(ExpectedConditions.ElementExists(By.CssSelector("a[href='eventLog.cgi?command=0']")));
driver.FindElement(By.CssSelector("a[href='eventLog.cgi?command=0']")).Click();

driver.SwitchTo().Frame("content");
WebDriverWait wait2 = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait2.Until(ExpectedConditions.ElementExists(By.CssSelector("a[href='cgi-bin/terminal.cgi']")));
IWebElement baseTable = driver.FindElement(By.ClassName("TableText1"));
// gets all table rows
ICollection<IWebElement> rows = baseTable.FindElements(By.TagName("tr"));
// for every row
IWebElement matchedRow = null;
foreach (var row in rows)
{
    Console.WriteLine(row.FindElement(By.XPath("td/a")).GetAttribute("href"));
}

first no content found in .Frame("content"), if this commented, then time out in .FromSeconds(10). if this is commented then no element found in .FindElement(By.ClassName("TableText1"). the html code is here

<table class="TableText1" nowrap="">
<tbody><tr>

<tr>
<td class="Table_Header">&nbsp;</td><td class="Table_Header"><a href="eventLog.cgi?command=0">User ID</a></td>
<td class="Table_Header"><a href="eventLog.cgi?command=0">Name</a></td>
<td class="Table_Header"><a href="eventLog.cgi?command=0">Department</a></td>
<td class="Table_Header"><a href="eventLog.cgi?command=0">Date Time<img src="/sort_arrow_down.gif" border="0"></a></td>


<tr nowrap=""><td class="Table_List">1</td><td class="Table_List"> <a href="employee.cgi?action=modify&amp;rid=14">319</a></td><td class="Table_List">Abul Hosain</td><td class="Table_List">General</td><td class="Table_List">2017/01/29 15:22:33</td></tr>
<tr nowrap=""><td class="Table_List_diff">2</td><td class="Table_List_diff"> <a href="employee.cgi?action=modify&amp;rid=49">310</a></td><td class="Table_List_diff">Tabassum Tamanna</td><td class="Table_List_diff">General</td><td class="Table_List_diff">2017/01/29 15:19:50</td></tr>
<tr><td colspan="10"><hr></td></tr>
<tr>
   
</tr></tbody></table>

Upvotes: 2

Views: 12515

Answers (3)

KR Akhil
KR Akhil

Reputation: 1017

Below is the C# code to read table content

//tbody
IWebElement diagnosticsTableBody = _browser.FindElement(By.XPath("//div[contains(text(),'Diagnostics')]/../..//tbody"));

            List<List<string>> diagnosticsTableContent = new List<List<string>>();
            
            int diagnosticsTableRowsCount = diagnosticsTableBody.FindElements(By.XPath("./tr")).Count;

            for (int i = 0; i < diagnosticsTableRowsCount; i++)
            {
                List<IWebElement> rowElements = diagnosticsTableBody.FindElements(By.XPath($".//tr[{i+1}]//td")).ToList();
                List<string> diagnosticsRowContent = new List<string>();
                for (int j  = 0; j < rowElements.Count; j++)
                {
                    diagnosticsRowContent.Add(rowElements[j].Text);
                }
                diagnosticsTableContent.Add(diagnosticsRowContent);
            }
            return diagnosticsTableContent;

Upvotes: 1

NarendraR
NarendraR

Reputation: 7708

Hope you are switching right frame. If there are more then one frame in your doc and you have switched in frame 1 then first you need to switch back from frame one like following -

driver.SwitchTo().DefaultContent();  

and then switch in another frame and do your actions

Use the following code to extract your table data -

IList<IWebElement> allElement = driver.FindElements(By.TagName("td"));
 foreach (IWebElement element in allElement )
 {
    string cellText= element.Text;
    Console.WriteLine(cellText);
 }

Upvotes: 4

ymz
ymz

Reputation: 6924

I suspect the issue is different time-frames... in short:

The table may be rendered by client libs (like angular, react etc.) or requested from the server by ajax call...

Please use a delay before inspecting the content - give your page some time to load itself properly. (especially when dealing with tables or other controls which contain lot of information)

Upvotes: -1

Related Questions