我之前问了一个关于XPath的问题,我试图从中解决我的另一个问题,但它没有成功。
现在我想获得一些链接的href值,稍后我会迭代它。这些链接位于<td>
该div是一个表格,<div>
- 元素是来自<td class="link ac">
的元素 - 元素
ICollection<IWebElement> matchLinks =
phantomDriver.FindElements(By.XPath("//*[contains(@class,'right-body-2')]//td[contains(concat(' ',normalize-space(@class),' '), ' ac ')]/a/@href"));
中有多个链接,但我只需要父//div[contains(@class,'col-lg-8')]//td[contains(@class, 'link ac')]/a/hrefs
的链接。空间有点棘手,但我找到了解决方案(至少我是这么认为的)。但是,它仍然无法工作。
foreach (IWebElement match in matchLinks)
{
rtb_matches.Text += match.Text + Environment.NewLine;
}
错误在哪里?
更新:
解决方案
calculate the average of values
给了我一个空的集合。我尝试在RichTextBox中编写提取的href,但框仍为空。
angular.module('app', [])
.controller('myCtrl', function ($scope) {
$scope.data = [
{
"0":"1",
"1":"Creativity",
"2":"5",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"1",
"Title":"Creativity",
"Value":"5",
"Parent":"1",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":null
},
{
"0":"2",
"1":"Courage",
"2":"4",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"2",
"Title":"Courage",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":null
},
{
"0":"3",
"1":"Honnesty",
"2":"4",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"3",
"Title":"Honnesty",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":null
},
{
"0":"4",
"1":"Ambition",
"2":"3",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"4",
"Title":"Ambition",
"Value":"3",
"Parent":"1",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":null
},
{
"0":"5",
"1":"Integrity",
"2":"4",
"3":"1",
"4":"2017-01-17 21:24:42",
"5":"ratings",
"6":null,
"ID":"5",
"Title":"Integrity",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-17 21:24:42",
"Category":"ratings",
"Author":null
},
{
"0":"165",
"1":"Creativity",
"2":"4",
"3":"1",
"4":"2017-01-20 21:12:25",
"5":"ratings",
"6":"18",
"ID":"165",
"Title":"Creativity",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-20 21:12:25",
"Category":"ratings",
"Author":"18"
},
{
"0":"166",
"1":"Courage",
"2":"4",
"3":"1",
"4":"2017-01-20 21:12:25",
"5":"ratings",
"6":"18",
"ID":"166",
"Title":"Courage",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-20 21:12:25",
"Category":"ratings",
"Author":"18"
},
{
"0":"167",
"1":"Honnesty",
"2":"2",
"3":"1",
"4":"2017-01-20 21:12:25",
"5":"ratings",
"6":"18",
"ID":"167",
"Title":"Honnesty",
"Value":"2",
"Parent":"1",
"Timestamp":"2017-01-20 21:12:25",
"Category":"ratings",
"Author":"18"
},
{
"0":"168",
"1":"Ambition",
"2":"3",
"3":"1",
"4":"2017-01-20 21:12:25",
"5":"ratings",
"6":"18",
"ID":"168",
"Title":"Ambition",
"Value":"3",
"Parent":"1",
"Timestamp":"2017-01-20 21:12:25",
"Category":"ratings",
"Author":"18"
},
{
"0":"169",
"1":"Integrity",
"2":"2",
"3":"1",
"4":"2017-01-20 21:12:25",
"5":"ratings",
"6":"18",
"ID":"169",
"Title":"Integrity",
"Value":"2",
"Parent":"1",
"Timestamp":"2017-01-20 21:12:25",
"Category":"ratings",
"Author":"18"
},
{
"0":"170",
"1":"Creativity",
"2":"4",
"3":"1",
"4":"2017-01-20 21:16:53",
"5":"ratings",
"6":"18",
"ID":"170",
"Title":"Creativity",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-20 21:16:53",
"Category":"ratings",
"Author":"18"
},
{
"0":"171",
"1":"Courage",
"2":"4",
"3":"1",
"4":"2017-01-20 21:16:53",
"5":"ratings",
"6":"18",
"ID":"171",
"Title":"Courage",
"Value":"4",
"Parent":"1",
"Timestamp":"2017-01-20 21:16:53",
"Category":"ratings",
"Author":"18"
},
{
"0":"172",
"1":"Honnesty",
"2":"5",
"3":"1",
"4":"2017-01-20 21:16:53",
"5":"ratings",
"6":"18",
"ID":"172",
"Title":"Honnesty",
"Value":"5",
"Parent":"1",
"Timestamp":"2017-01-20 21:16:53",
"Category":"ratings",
"Author":"18"
},
{
"0":"173",
"1":"Ambition",
"2":"3",
"3":"1",
"4":"2017-01-20 21:16:53",
"5":"ratings",
"6":"18",
"ID":"173",
"Title":"Ambition",
"Value":"3",
"Parent":"1",
"Timestamp":"2017-01-20 21:16:53",
"Category":"ratings",
"Author":"18"
},
{
"0":"174",
"1":"Integrity",
"2":"5",
"3":"1",
"4":"2017-01-20 21:16:53",
"5":"ratings",
"6":"18",
"ID":"174",
"Title":"Integrity",
"Value":"5",
"Parent":"1",
"Timestamp":"2017-01-20 21:16:53",
"Category":"ratings",
"Author":"18"
}
];
$scope.titles = _.uniq(_.map($scope.data, 'Title'));
$scope.modifiedData = [];
angular.forEach($scope.titles, function (title){
var eachCategory = _.filter($scope.data, {'Title': title});
var sum = 0;
_.map(eachCategory, function (each){
sum += parseInt(each.Value);
});
if(!$scope.modifiedData[title]){
$scope.modifiedData.push({'Title': title, 'Value': +(sum/eachCategory.length).toFixed(2)});
}
});
});
答案 0 :(得分:0)
这些是多个类,因为类中不能包含空格。因此,您的节点实际上有两个类:link
和ac
要查找此节点,您只需使用contains
函数添加多个选项,如下所示:
"//td[contains(@class, 'link')][contains(@class, 'ac')]"
或简单地将两者结合起来,这更短但只有在课程顺序没有改变时才有效(很少见):
"//td[contains(@class, 'link ac')]"
答案 1 :(得分:0)
经过一番反复试验后,我发现以下解决方案适合我。
//div[contains(@class,'right-body-2')]//tbody//td[contains(@class, 'link ac')]/a
出于输出目的,我使用了
foreach (IWebElement match in matchLinks)
{
rtb_matches.Text += match.GetAttribute("href") + Environment.NewLine;
}
我希望这可以帮助任何有类似问题的人,即使它可能远非美丽和漂亮:D 感谢Granitosaurus帮助我:)