繁体   English   中英

需要帮助-计算单词“ cheese”字符串出现的次数,并使用硒在java中打印数字

[英]Need Help - Count number of times the word “cheese” string appears and print number in java using selenium

嗨,我正试图弄清楚如何计算单词“ cheese”字符串出现的次数,并使用硒打印数字。 这是我所拥有的:

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.bing.com");
    WebElement element = driver.findElement(By.name("q")); 
    element.sendKeys("cheese"); 
    element.submit(); 


    List <WebElement> links = driver.findElement(By.xpath("//*")).findElements(By.tagName("a"));


    for(int i = 0; i < links.size(); i++)
    {
        System.out.println("Link: " + links.get(i).getAttribute("href"));
        System.out.println("Title" + links.get(i).getAttribute("title"));
        System.out.println("Description: " + links.get(i).getText()+ "\n");
        //Trying to calculate the total count the word "cheese" from .getText()     
    }

任何帮助表示赞赏。 谢谢

如果正确编写选择器,应该很容易。 请遵循以下步骤:

By byXpath = ".//a/strong[.='cheese' or .='Cheese']" //assuming you want all the cheese ignoring case

//and then find the list and count

IList<IWebElement> list = Driver.FindElements(byXpath);
Console.WriteLine(list.Count.ToString());

使用C#

StringBuilder stringBuilder = new StringBuilder();

for(int i = 0; i < links.size(); i++) {
    stringBuilder.append(links.get(i).getText());
}

String totalResult = stringBuilder.toString();

System.out.println(totalResult.toLowerCase().split("cheese", -1).length-1);

暂无
暂无

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

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