2017-01-18 92 views

回答

0

嗨你能提供一個示例表結構,只是你的HTML如何。看起來您正在使用角度2控件。這就是控件看起來像一張桌子的地方,但是如果您掃描它,它的所有嵌套div標籤。我在處理這個問題之前,我只需要您的HTML結構,以便我可以發佈解決方案。 如果你的表格結構是這樣的

<div> 
    <div> 
    <div></div> 
    <div></div> 
    <div></div> 
    </div> 
    <div> 
    <div></div> 
    <div></div> 
    <div></div> 
    </div> 
    <div> 
    <div></div> 
    <div></div> 
    <div></div> 
    </div> 
</div> 



public void IterateThroughDivTable(IWebElement tblParentObj) 
     { 
      var childCount = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].childNodes.length; ", tblParentObj); 
      for (int i = 0; i < Convert.ToInt32(childCount); i++) 
      { 
       var rowDivTag = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].childNodes["+i+"].tagName; ", tblParentObj); 

       //check to see if its a div tag else ignore it 
       if (rowDivTag.ToString() == "div")//this is your row 
       { 
        var tr = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].childNodes[" + i + "]; ", tblParentObj); 
        //now try to find the nested div tag inside each tr div tag 
        foreach (IWebElement tc in (IEnumerable)tr) 
        { 
         var colDivTag = ((IJavaScriptExecutor)DriverContext.Driver).ExecuteScript("return arguments[0].childNodes[" + i + "].tagName; ", tc); 
         //check to see if its a div tag else ignore it 
         if (colDivTag.ToString() == "div")//this is your column in each row 
         { 
          //now this is each cell in the table 
          var divText = tc.Text; 
         } 
        } 
       } 

      } 
     }