HTML XPath 按类和文本搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16466083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:15:05  来源:igfitidea点击:

HTML XPath Searching by class and text

htmlxpath

提问by user1786107

I want to find all elements in xpath by class and text. I have tried this but it does not work.

我想按类和文本查找 xpath 中的所有元素。我试过这个,但它不起作用。

//*[contains(@class, 'myclass')]//*[text() = 'qwerty']

I am trying to find all elements that have a class of 'myclass' and the text is 'qwert' (these will be span elements)

我试图找到所有具有“myclass”类并且文本为“qwert”的元素(这些将是跨度元素)

回答by Tomalak

//span[contains(@class, 'myclass') and text() = 'qwerty']

or

或者

//span[contains(@class, 'myclass') and normalize-space(text()) = 'qwerty']

回答by Evdzhan Mustafa

Generic solution for anyone looking for a XPath expression to search based on class and text:

适用于寻找 XPath 表达式以根据类和文本进行搜索的任何人的通用解决方案:

Class is only"myclass":

班级只是“我的班级”:

//*[@class='myclass' and contains(text(),'qwerty')]

Class contains"myclass":

包含“myclass”:

//*[contains(@class,'myclass') and contains(text(),'qwerty')]