Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

javascript - 根据内部文本选择div时出现问题(Problems selecting a div depending on text inside)

i've been working on a project with puppeteer and it was going great so far until a tried to localize div's depending on their text.(我一直在与puppeteer一起进行一个项目,到目前为止,进展非常顺利,直到尝试根据其文本本地化div为止。)

I can't use the div id, name or class, i need to find it by the text inside of it.(我不能使用div ID,名称或类,我需要通过其中的文本来查找它。) My idea is to create an array of all the text i need to find and then loop trought the div's to find the ones that match any of the text in the array.(我的想法是创建一个我需要查找的所有文本的数组,然后循环遍历div以查找与该数组中的任何文本匹配的文本。) Can anybody help me find a solution?(有人可以帮我找到解决方案吗?) Thank you!(谢谢!)   ask by antonella manzur translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Matching by text isn't something you can do with plain CSS Selectors, but it is supported in jQuery and xPath, if they are possible for you to use.(使用纯文本选择器无法实现按文本匹配,但是如果您可以使用,则jQuery和xPath支持它。)

The jQuery :contains() selector:(jQuery :contains()选择器:)

$('div:contains("yourText")')

The xPath text() selectors(xPath text()选择器)

$x('//div[text()="yourText"]')            // Match
$x('//div[contains(text(), "yourText")]') // Substring / Contains

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...