Module-13 Excercise --- sum the mark brands Qty | Selenium Forum
M
Posted on 24/05/2016
Hi,
I want to sum the mark brands Qty. from the list, please see the attachment for the same.

[b:1sq5nut3]Code Logic[/b:1sq5nut3]:-


import java.sql.Driver;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CountLinks {

public static void main(String[] args) {

int sum=0,sum1;
int _startingIndex = 0,_endingIndex = 0,show=0,i;
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[3]/div/div[1]/a")).click();
WebElement panel= driver.findElement(By.xpath("//*[@class='refinement brand']"));
List<WebElement> subpanel= panel.findElements(By.tagName("a"));
//System.out.println("Totla Links --> " + subpanel.size());
//int _startingIndex,_endingIndex,show=0;
for(i=0;i<=subpanel.size();i++)
{

if(subpanel.get(i).isDisplayed() != false){
System.out.println("Links value -- >" + subpanel.get(i).getText() +" -----> "+ subpanel.get(i).isDisplayed());
_startingIndex=subpanel.get(i).getText().indexOf('(');
_endingIndex=subpanel.get(i).getText().indexOf(')');
System.out.println(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));

//sum1= Integer.parseInt(sum+subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//show += Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//System.out.println("Total Number of Records are -- >>" +show);
// sum1= sum1 + Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//System.out.println("Total Number of Records are -- >>" +sum1);

}
}
show += Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
System.out.println("Total Number of Records are -- >>" +show);
}
}
[b:1sq5nut3]
NOTE:- In the attached output my code is count one button name 'Collapse' and after that display 'String index out of range' message. i think due to that i am not able to sum the total number of Qty.of each brand.[/b:1sq5nut3]. So please suggest me where i am wrong. i got stuck from last two days and try allot to fix it but not able to do.

M
Replied on 24/05/2016

what is the problem? can you explain again?


M
Replied on 24/05/2016

I want to sum the Qty. of all products which are display under "Brand". problem is that when i execute my code it display an error " STRING
DISPLAY OUT OF RANGE : -1 " .

[b:1aqtyi15]Please refer my previous attached screen shots [/b:1aqtyi15]


[b:1aqtyi15]Code :-[/b:1aqtyi15]


import java.sql.Driver;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class CountLinks {

public static void main(String[] args) {

int sum=0,sum1;
int _startingIndex = 0,_endingIndex = 0,show=0,i;
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[3]/div/div[1]/a")).click();
WebElement panel= driver.findElement(By.xpath("//*[@class='refinement brand']"));
List<WebElement> subpanel= panel.findElements(By.tagName("a"));
//System.out.println("Totla Links --> " + subpanel.size());
//int _startingIndex,_endingIndex,show=0;
for(i=0;i<=subpanel.size();i++)
{

if(subpanel.get(i).isDisplayed() != false){
System.out.println("Links value -- >" + subpanel.get(i).getText() +" -----> "+ subpanel.get(i).isDisplayed());
_startingIndex=subpanel.get(i).getText().indexOf('(');
_endingIndex=subpanel.get(i).getText().indexOf(')');
System.out.println(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));

//sum1= Integer.parseInt(sum+subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//show += Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//System.out.println("Total Number of Records are -- >>" +show);
// sum1= sum1 + Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
//System.out.println("Total Number of Records are -- >>" +sum1);

}
show += Integer.parseInt(subpanel.get(i).getText().substring(_startingIndex+1,_endingIndex));
}
System.out.println("Total Number of Records are -- >>" +show);
}
}


M
Replied on 24/05/2016

I please ignore this. I have fixed the issue. Responsive image

Thanks.