2015-05-27 80 views
0

表数据我有HTML与看起来基本上就像在C#以下获取与XPath和硒

.... 
<div id="a"> 
<table class="a1"> 
<tbody> 
<tr> 
<td><a href="a11.html>a11</a> 
</tr> 
<tr> 
<td><a href="a12.html>a12</a> 
</tr> 
</tbody> 
<table> 
</div> 
... 

下面的编码我用,但我不能在这个阶段中获取的网址

IWebElement baseTable = driver.FindElement(By.ClassName(TableID)); 
// gets all table rows 
ICollection<IWebElement> rows = baseTable.FindElements(By.TagName("tr")); 
// for every row 
IWebElement matchedRow = null; 
foreach(var row in rows) 
{ 
Console.Write (row.FindElements(By.XPath("td/a"))); 
} 

回答

0

这看起来并不像一个有效的XPath我

Console.Write (row.FindElements(By.XPath("td/a"))); 

尝试

Console.Write (row.FindElements(By.XPath("/td/a"))); 
3

首先,你给我们无效的标记。正确的:

<div id="a"> 
 
    <table class="a1"> 
 
     <tbody> 
 
     <tr> 
 
      <td> 
 
       <a href="a11.html">a11</a> 
 
      </td> 
 
     </tr> 
 
     <tr> 
 
      <td> 
 
       <a href="a12.html">a12</a> 
 
      </td> 
 
     </tr> 
 
     </tbody> 
 
    </table> 
 
</div>

如果你在表行只有一个锚,你应该使用这个代码来检索网址:

IWebElement baseTable = driver.FindElement(By.ClassName(TableID)); 
// 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"));  
} 

你需要得到的发现href属性元件。否则,row.FindElement(By.XPath("td/a")将会打印类型名称的IWebElement继承类,因为它是某种类型的对象,而不是字符串。

+0

无法找到元素:{“method”:“xpath”,“selector”:“/ td/a /”} –