如何在c#selenium中获取表格数据并将其保存到任何文档中 driver.SwitchTo().Frame(“菜单”); WebDriverWait wait1=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(15)); wait1.Until(ExpectedConditions.element存在(由.CssSelector(“a[href='eventLog.cgi?command=0'])); driver.findelelement(通过.CssSelector(“a[href='eventLog.cgi?command=0']))。单击(); driver.SwitchTo().Frame(“内容”); WebDriverWait wait2=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(10)); wait2.Until(ExpectedConditions.element存在(由.CssSelector(“a[href='cgi-bin/terminal.cgi'])); IWebElement baseTable=driver.FindElement(By.ClassName(“TableText1”); //获取所有表行 ICollection rows=baseTable.FindElements(按.TagName(“tr”)); //每行 IWebElement matchedRow=null; foreach(行中的变量行) { Console.WriteLine(row.findelelement(By.XPath(“td/a”)).GetAttribute(“href”); }

如何在c#selenium中获取表格数据并将其保存到任何文档中 driver.SwitchTo().Frame(“菜单”); WebDriverWait wait1=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(15)); wait1.Until(ExpectedConditions.element存在(由.CssSelector(“a[href='eventLog.cgi?command=0'])); driver.findelelement(通过.CssSelector(“a[href='eventLog.cgi?command=0']))。单击(); driver.SwitchTo().Frame(“内容”); WebDriverWait wait2=新的WebDriverWait(驱动程序,TimeSpan.FromSeconds(10)); wait2.Until(ExpectedConditions.element存在(由.CssSelector(“a[href='cgi-bin/terminal.cgi'])); IWebElement baseTable=driver.FindElement(By.ClassName(“TableText1”); //获取所有表行 ICollection rows=baseTable.FindElements(按.TagName(“tr”)); //每行 IWebElement matchedRow=null; foreach(行中的变量行) { Console.WriteLine(row.findelelement(By.XPath(“td/a”)).GetAttribute(“href”); },c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,首先,在.Frame(“内容”)中找不到任何内容,如果该内容已注释,则在.FromSeconds(10)中超时。如果对此进行了注释,则在.FindElement(By.ClassName(“TableText1”)中找不到元素。 html代码在这里 阿布医院2017/01/29 15:22:33 2 Tabassum Tamanna概述2017/01/29 15:19:50 我怀疑问题在于不同的时间框架……简言之: 该表可以由客户端libs(如angular、react等)呈现,也可以通过

首先,在.Frame(“内容”)中找不到任何内容,如果该内容已注释,则在.FromSeconds(10)中超时。如果对此进行了注释,则在.FindElement(By.ClassName(“TableText1”)中找不到元素。 html代码在这里


阿布医院2017/01/29 15:22:33
2 Tabassum Tamanna概述2017/01/29 15:19:50


我怀疑问题在于不同的时间框架……简言之:

该表可以由客户端libs(如angular、react等)呈现,也可以通过ajax调用从服务器请求


请在检查内容之前延迟一段时间-给您的页面一些时间来正确加载它自己(特别是在处理包含大量信息的表格或其他控件时)

希望您正在正确切换
。如果您的文档中有多个帧,并且您已切换到第1帧,则首先需要从第1帧切换回,如下所示-

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"));
}
然后切换到另一个
框架
,并执行您的操作

使用以下代码提取表格数据-

driver.SwitchTo().DefaultContent();  
IList-allegement=driver.FindElements(按.TagName(“td”));
foreach(等位基因中的IWebElement元素)
{
字符串cellText=element.Text;
Console.WriteLine(cellText);
}
下面是读取表格内容的C代码

IList<IWebElement> allElement = driver.FindElements(By.TagName("td"));
 foreach (IWebElement element in allElement )
 {
    string cellText= element.Text;
    Console.WriteLine(cellText);
 }
//t车身
IWebElement diagnosticsTableBody=_browser.FindElement(By.XPath(“//div[contains(text(),'Diagnostics')]/../..//tbody”);
List diagnosticsTableContent=新建列表();
int diagnosticstablerowscont=diagnosticsTableBody.FindElements(By.XPath(“./tr”)).Count;
对于(int i=0;i
请将html添加到html中page@Tamim,您能否更新问题内容并清楚地说明您想要实现的目标?您的问题不是自描述性的。@narendrajput我只想获取表格数据并将其保存在文本文档中。
//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;