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 /」} –