如何使用Selenium Webdriver和C#获取具有ID在DIV标记内的表元素的总数

时间:2015-07-06 07:22:09

标签: c#-4.0 selenium selenium-webdriver

如何使用Selenium Webdriver和C#获取DIV标记内具有ID的表元素总数?

预期输出:我需要输出为“表ID = 2”

 <form id="form1"> 
   <div class="sec_container_pop">  
   <div class="sec_header_pop"> Item Details </div> 
   <table class="subheader">
   <div class="spacerdiv"/> 
   <div style="width: 900px; height: 400px; overflow-x: auto; position: relative; overflow-y: auto;">
   <table class="reportscontent_pop" style="width: 880px"> 
   <table id="tItemDetails0" class="reportscontent_pop" style="width: 880px; display: none;"> 
   <table class="reportscontent_pop" style="width: 880px"> 
   <table id="tItemDetails1" class="reportscontent_pop" style="width: 880px; display: none;"> 
</div>
</form>

2 个答案:

答案 0 :(得分:1)

试试这样:

WebElement webElement = driver.findElement(By.xpath("//form[@id='form1']/div[4]"));

//Get list of table elements using tagName
List<WebElement> list = webElement.findElements(By.tagName("table"));

答案 1 :(得分:0)

的XPath:

IList<IWebElement> tableIDs = driver.FindElements(By.XPath("//div/table[@id]"));

Console.WriteLine("Number of Table ID = {0}", tableIDs.Count);

的CSS:

IList<IWebElement> tableIDs = driver.FindElement(By.CssSelector("div>table[id]"));

Console.WriteLine("Number of Table ID = {0}", tableIDs.Count);