简体   繁体   English

java-regex:如何为两位数数字写正则表达式

[英]java-regex: How to write regular expression for two digit numbers

I have password field with ID locator "j_idt90". 我具有ID定位符“ j_idt90”的密码字段。 However the ID is dynamic and the two digits in the preceeding changes everytime login page loads. 但是,该ID是动态的,并且每次登录页面加载时,前面的两位数字都会更改。

Iam using automation to capture this field and used below regular expression but it is failing. Iam使用自动化来捕获此字段,并在正则表达式下使用,但失败了。 Do, let me know where I'm failing to identify the element. 可以,让我知道我在哪里找不到该元素。

Reg Exp - driver.findElement(By.id("j_idt[0-9]{2}")); Reg Exp- driver.findElement(By.id("j_idt[0-9]{2}"));

You can locate the element by XPath instead. 您可以改为通过XPath定位元素。 Assuming that there is only one password field in your page, the following snippet should work: 假设页面中只有一个密码字段,则以下代码段应该起作用:

driver.findElement(By.xpath("//input[@type='password']"));

By.id does not take a regular expression, but a simple string as argument. By.id不需要正则表达式,而是一个简单的字符串作为参数。

See here: https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html#id-java.lang.String- 参见此处: https : //seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/By.html#id-java.lang.String-

Similar to Boni's answer, but you could try using the xpath function starts-with 与Boni的答案类似,但是您可以尝试使用xpath函数starts-with

driver.findElement(By.xpath("//input[starts-with(@id, 'j_idt')]"));

This will search for any "input" elements which have an ID starting with the text 'j_idt'. 这将搜索具有以文本“ j_idt”开头的ID的所有“输入”元素。 See also http://www.w3schools.com/xml/xsl_functions.asp 另请参阅http://www.w3schools.com/xml/xsl_functions.asp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM