gpt4 book ai didi

java - Selenium 代码查找段落中的字数

转载 作者:太空宇宙 更新时间:2023-11-04 12:40:41 24 4
gpt4 key购买 nike

您好,我正在尝试计算段落中的字数。以下是我查找计数的代码。请检查代码并告诉我错误。

Code
----
package checking;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class WordCount {

public static void main(String[] args) {
RemoteWebDriver driver;

//driver = new FirefoxDriver();

System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
driver = new ChromeDriver();

driver.manage().window().maximize();
driver.get("https://jqueryui.com/");

WebElement Para = driver.findElement(By.xpath("//*[@id='banner-secondary']/p"));
String ParaCon = Para.getText();

System.out.println("Paragraph Content : "+ParaCon);

int ParaConCount = ParaCon.length();
System.out.println("Paragraph Characters Count : "+ParaConCount);

int count=0;
for (int i = 0; i <= ParaConCount; i++) {
if (ParaCon.contains("of")) {
count++;
break;
}
}
System.out.println("Count of Word present : "+count);

driver.quit();
}

}

它应该得到所给出的单词的计数

最佳答案

使用示例:

ParaCon = "of of of"

这会给你:

ParaConCount = 8

一个 for 循环如下所示:

int count = 0;
for (int i = 0; i <= 8; i++) {
if ("of of of".contains("of")) {
count++; // count is now 1
break; // We get out of the loop here (so we have count = 1)
}
}
// count is now 1
<小时/>

使用怎么样:

StringUtils.countMatches(ParaCon, "of"); // this will return 3

关于java - Selenium 代码查找段落中的字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36861426/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com