使用XPath查找元素(Find element with XPath)
我有以下标记:
<div> <div>Question1</div> <div> <button type="button">No</button> <button type="button">Yes</button> </div> </div> <div> <div>Question2</div> <div> <button type="button">No</button> <button type="button">Yes</button> </div> </div>
以下XPath表达式找到两个yes按钮:
//div/div/button[text()='Yes']
但是我只想要第一个div的
Yes
按钮,如://div/div[text()='Question1']
我怎么能找到它?
I have the following markup:
<div> <div>Question1</div> <div> <button type="button">No</button> <button type="button">Yes</button> </div> </div> <div> <div>Question2</div> <div> <button type="button">No</button> <button type="button">Yes</button> </div> </div>
The following XPath expression locates two yes buttons:
//div/div/button[text()='Yes']
However I only want the
Yes
button of the first div as in://div/div[text()='Question1']
How can I locate that?
原文:https://stackoverflow.com/questions/10281069
更新时间:2023-06-25 22:06
最满意答案
反向引用需要转义
Pattern pattern = Pattern.compile("(.).*\\1");
The backreference needs to be escaped
Pattern pattern = Pattern.compile("(.).*\\1");
相关问答
更多-
简单的答案是在真实角色之前和之后允许任意数量的允许角色。 即^[-,.'\s\p{L}]*\p{L}[-,.'\s\p{L}]*$ 问候 The simple answer is to allow any number of your allowed characters before, and after, a real character. I.e. ^[-,.'\s\p{L}]*\p{L}[-,.'\s\p{L}]*$ Regards
-
强密码正则表达式(strong password regex)[2022-12-09]
这是一个PCRE / Perl正则表达式,可以完成所有这些工作: / ^ # anchor it # must be at least 8 characters (?=.{8,}) # must have characters from at least 3 of the 4 different character types (?: (?=.*\d)(?=.*[A-Z])(?=.*[a-z]) | (?=.*\d)(?=.*[^A-Za-z0-9\s])(?=.*[a-z]) | (?=.*[^ ... -
正则表达式检测查询字符串中的反斜杠,数字和特殊字符(Regex to detect back slashes, numbers and special characters in a query string)[2023-04-13]
像这样的东西? [\\\w+\d+\;\-]+ https://regex101.com/r/zD2oH8/1 Something like this? [\\\w+\d+\;\-]+ https://regex101.com/r/zD2oH8/1 -
根据你的问题,使用如下: public class PatternIncorrect { public static void main(String[] args) { System.out.println("------Test 1------------- "); System.out.println(test1("0x4F 0xBB 0x74 0xA9")); System.out.println("------Test 2----- ...
-
就如此容易 function isIsogram (str) { return !/(\w).*\1/i.test(str); } as simple as that function isIsogram (str) { return !/(\w).*\1/i.test(str); }
-
反向引用需要转义 Pattern pattern = Pattern.compile("(.).*\\1"); The backreference needs to be escaped Pattern pattern = Pattern.compile("(.).*\\1");
-
RegEx Pattern不是字符串 从MDN文档 : 有两种方法可以创建一个RegExp对象:文字符号和构造函数。 要指示字符串,文本表示法的参数不使用引号,而构造函数的参数使用引号。 因此,以下表达式创建相同的正则表达式: /ab+c/i; new RegExp('ab+c', 'i'); new RegExp(/ab+c/, 'i'); 不同的方法有不同的优点和缺点。 function palindrome(str) { //Clean up string var re = /[^a-z0 ...
-
在正则表达式的两端添加锚点 ,加上量词(如果要排除空字符串,则为+ ;如果要允许空字符串,则为* )。 现在,你只是检查第一个字符(单数)是否是字母(即匹配[a-zA-Z] )。 你想要的是: re.match("^[a-zA-Z]+$", textToRestrict) (或者,如果您的过滤器真的很简单,请考虑使用像str.isalpha这样的字符串方法, 正如SirParselot在评论中所建议的那样 。) Add anchors at both ends of the regex, plus a q ...
-
为了能够判断RE是否“合法”,您需要能够“计算括号”来检查它们是否平衡,而RE(或FA)则无法做到这一点。 To be able to decide if a RE is "legit", you would need to be able to "count parentheses" to check they are balanced, which you can NOT do with a RE (or FA).
-
正则表达式检测由非字母字符分隔的字符串(或什么都不是)(regex to detect string separated by non alphabet characters (or nothing))[2021-08-10]
使用正则表达式(\\b|[^[:alpha:]])el(\\b|[^[:alpha:]])和grepl : > tests <- c("el", "hello", "123el", "el/27") > y <- grepl("(\\b|[^[:alpha:]])el(\\b|[^[:alpha:]])", tests) > y [1] TRUE FALSE TRUE TRUE el是否作为实体出现的条件是双方要么具有单词边界( \b ),要么具有非字母字符(由R中的字符类[^[:alpha:]] ...