Java 文本在网页中出现的次数 - Selenium Webdriver

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18442895/
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-12 01:39:53  来源:igfitidea点击:

How many times a text appears in webpage - Selenium Webdriver

javaseleniumselenium-webdriver

提问by Nandini Joshi

Hi I would like to count how many times a text Ex: "VIM LIQUID MARATHI" appears on a page using selenium webdriver(java). Please help.

嗨,我想计算文本 Ex: "VIM LIQUID MARATHI" 出现在使用 selenium webdriver(java) 的页面上的次数。请帮忙。

I have used the following to check if a text appears in the page using the following in the main class

我已经使用以下内容在主类中使用以下内容检查页面中是否出现文本

assertEquals(true,isTextPresent("VIM LIQUID MARATHI"));

assertEquals(true,isTextPresent("VIM LIQUID MARATHI"));

and a function to return a boolean

和一个返回布尔值的函数

protected boolean isTextPresent(String text){
    try{
        boolean b = driver.getPageSource().contains(text);
        System.out.println(b);
        return b;
    }
    catch(Exception e){
        return false;
    }
}

... but do not know how to count the number of occurrences...

...但不知道如何计算出现次数...

采纳答案by Dingredient

The problem with using getPageSource(), is there could be id's, classnames, or other parts of the code which match your String, but those don't actually appear on the page. I suggest just using getText()on the body element, which will only return the page's content, and not HTML. If I'm understanding your question correctly, I think that is more what you are looking for.

使用getPageSource(),的问题是可能有 id、类名或代码的其他部分与您的 String 匹配,但这些实际上并未出现在页面上。我建议只getText()在 body 元素上使用,它只会返回页面的内容,而不是 HTML。如果我正确理解您的问题,我认为这更符合您的要求。

// get the text of the body element
WebElement body = driver.findElement(By.tagName("body"));
String bodyText = body.getText();

// count occurrences of the string
int count = 0;

// search for the String within the text
while (bodyText.contains("VIM LIQUID MARATHI")){

    // when match is found, increment the count
    count++;

    // continue searching from where you left off
    bodyText = bodyText.substring(bodyText.indexOf("VIM LIQUID MARATHI") + "VIM LIQUID MARATHI".length());
}
System.out.println(count);

The variable countcontains the number of occurrences.

该变量count包含出现的次数。

回答by kaos

You can try to execute javascript expression using webdriver:

您可以尝试使用 webdriver 执行 javascript 表达式:

((JavascriptExecutor)driver).executeScript("yourScript();");

If you are using jQuery on your page you can use jQuery's selectors:

如果您在页面上使用 jQuery,则可以使用 jQuery 的选择器:

((JavascriptExecutor)driver).executeScript("return jQuery([proper selector]).size()");

[proper selector] - this should be selector that will match text you are searching for.

[适当的选择器] - 这应该是将匹配您正在搜索的文本的选择器。

回答by Nathan Merrill

There are two different ways to do this:

有两种不同的方法可以做到这一点:

int size = driver.findElements(By.xpath("//*[text()='text to match']")).size();

This will tell the driver to find all of the elements that have the text, and then output the size.

这将告诉驱动程序找到所有包含文本的元素,然后输出大小。

The second way is to search the HTML, like you said.

第二种方法是搜索 HTML,就像你说的。

int size = driver.getPageSource().split("text to match").length-1;

This will get the page source, the split the string whenever it finds the match, then counts the number of splits it made.

这将获取页面源,在找到匹配项时拆分字符串,然后计算它所做的拆分次数。

回答by Abhishek Singh

Try

尝试

int size = driver.findElements(By.partialLinkText("VIM MARATHI")).size();