[英]Select a child element from a parent using a cousin node of the same parent
TestCafe API非常智能,可以處理您的情況:
const JohnSelector = Selector('div.person-identity')
.find('span.person-name')
.withExactText('John');
const pathSelector = JohnSelector
.parent('div.projects-project-role')
.prevSibling('div.projects-project-role')
.find('button svg path')
.nth(1);
console.log(`${await JohnSelector.innerText}`);
console.log(`${await pathSelector.getAttribute("d")}`);
在jQuery
您可以使用類projects-project-role
獲取所有元素,然后在每個元素中搜索path
標記和類person-name
的元素,並將它們的值添加到數組中,如下所示:
var namePath = []; // initialize empty name/path array
$(".projects-project-role").each(function() { // go through each element of class projects-project-role
var path = $(this).find("path").attr("d"); // look for a path tag subelement and grab its d attribute
var name = $(this).find(".person-name").text(); // look for an element of class person-name and grab its text
namePath.push({path: path, name: name}); // add the gathered path and name to an array
});
console.log(namePath); // display the array
編輯:或者用速記,並用map
:
var namePath = $(".projects-project-role").get().map((p)=>({path: $(p).find('path').attr('d'), name: $(p).find('.person-name').text()}));
console.log(namePath); // display the array
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.