url - https://www.etmoney.com/mutual-funds/equity/focused/77
On the particular website I have to scape all the fund size and add all up and compare it later using Testng assertions. Challenge being that there are multiple delimiter on fund size (₹ , and space) 3 delimiters
for ex:(scraped fund size)
₹110 Crs
₹8,696 Crs
₹3,460 Crs
₹711 Crs
₹5,990 Crs
₹26,218 Crs
₹7,907 Crs
₹1,780 Crs
₹956 Crs
₹1,708 Crs
₹1,330 Crs
₹5,825 Crs
₹18,712 Crs
Final output e like
110
8696
3460
711
5990
26218
7907
1780
956
1708
1330
5825
18712
int total = 0;
List<WebElement> totalFunds = driver.findElements(By.xpath("//div[@class='card-bordy']/div/div/div/div/div[2]/div/div/div[2]/div/p[2]"));
for(int i=0;i<totalFunds.size();i ) {
String funds = totalFunds.get(i).getText();
String fu = funds.split(" ")[0];
System.out.println(fu.split("₹")[1]);
Error - "java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1"
CodePudding user response:
You could use a regex replacement/capture approach here:
int total = 0;
List<WebElement> totalFunds = driver.findElements(By.xpath("//div[@class='card-bordy']/div/div/div/div/div[2]/div/div/div[2]/div/p[2]"));
for (int i=0;i < totalFunds.size(); i ) {
String funds = totalFunds.get(i).getText();
String fu = funds.replaceAll(".*?(\\d{1,3}(?:,\\d{3})*).*", "$1");
System.out.println(fu);
}
CodePudding user response:
Your xpath selector can be simplified and made more robust with the css selector By.cssSelector(".fund-size .fund-amount")
.
And the value ₹3,460 Crs can be converted to a number with help of java.text.DecimalFormat
class.
List<WebElement> elements = driver
.findElements(By.cssSelector(".fund-size .fund-amount"));
DecimalFormat format = new DecimalFormat("₹###,### Crs");
for (WebElement element : elements) {
Number number = format.parse(element.getText());
System.out.println(number);
}
But take in mind that parse
method throws the ParseExteption
exception.