我正在尝试使用Eclipse中的Selenium使用“提交”按钮自动化工作流程。
我正在使用自定义函数waitForVisible
检查WebElement
是否带有id' naviInfo'显示,如果它包含一条消息,则表示没有找到任何行'或者' {number}行被发现'消息。
问题是我无法排序并检查文本的数字部分。示例代码如下。
String message = waitForVisible(By.id("naviInfo")).getText();
if ("No rows were found".equals(message)) {
log.info("No rows were found after submit");
}
else if ("**1804** rows were found".equals(message)) {
log.info("**1804** rows found after submit");
}
else {
(other error checks)
}
如何检查在公共文本之前是否找到了数字值'行被发现'?另外还将此数字保存到变量?
答案 0 :(得分:1)
如果我说得对,你只是问如何验证消息是否符合预期模式以及如何从字符串中提取数字?在这种情况下,这与Selenium无关,但这是一个简单的正则表达式问题。
Pattern p = Pattern.compile("^\\*{2}(\\d+)\\*{2} rows were found$"); //pattern that says: start of string, followed by two *s, then some digits, then two *s again, then the string " rows were found", and finally the end of string, capturing the digits only
Matcher m = p.matcher("**1804** rows were found");
boolean found = m.find(); //find and capture the pattern of interest
if (found)
int count = Integer.parseInt(m.group(1)); //get the first (and only) captured group, and parse the integer from it
使用Java here阅读正则表达式。
答案 1 :(得分:0)
所以这就是我使我的病情发挥作用的方式。
if (" no rows were found".equals(waitForVisible(By.id("naviInfo")).getText()))
waitForVisible(By.xpath("//td[contains(text(),'Nothing found to display.')]"));
else if (Pattern.matches("^ \\d+ rows were found$", waitForVisible(By.id("naviInfo")).getText()))
waitForVisible(By.xpath("//tbody//tr//td/a"));
else
other error checks