[英]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.