Module 13 exercise question 2(american-golf) | Selenium Forum
M
Posted on 08/03/2016
Hi ashish,

1. In module 13 there are some exercise which we have to practice in which i am facing an problem, can you tell me how can i get the sum of all the link text displayed in the brand menu.

2. I had tried to get each and every text from those link, but how can i add those text and compare them with total product in fairway woods.

3. Below is piece of code, can u help how can i add the text

[color=#FF0000:1zb0rhj7]package americanGOLF;

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.htmlunit.HtmlUnitDriver;

public class Findthetext_sum
{

public static void main(String[] args)
{

WebDriver Findsum= new HtmlUnitDriver();
Findsum.manage().window().maximize();
Findsum.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Findsum.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
List<WebElement> box = Findsum.findElements(By.xpath("//*[@id='secondary']/div[1]/div[3]/div/ul/li"));
System.out.println(box.size());



for (int i = 1; i < box.size()+1; i++)
{
WebElement firstlink = Findsum.findElement(By.xpath("//*[@id='secondary']/div[1]/div[3]/div/ul/li[" + i+ "]/a"));
//System.out.println(firstlink.getText());

//System.out.println("==================");
String text1 ="//*[@id='secondary']/div[1]/div[3]/div/ul/li[";
String text2 ="]/a/span[2]";

firstlink = Findsum.findElement(By.xpath(text1+i+text2));
System.out.println(firstlink.getText());


}
}

}
[/color:1zb0rhj7]

M
Replied on 09/03/2016

[quote:1r6ial0d]1. In module 13 there are some exercise which we have to practice in which i am facing an problem, can you tell me how can i get the sum of all the link text displayed in the brand menu.[/quote:1r6ial0d]


try some thing like this
List<webelement> links=driver.findelements(By.tagname("a"));


[quote:1r6ial0d]2. I had tried to get each and every text from those link, but how can i add those text and compare them with total product in fairway woods.[/quote:1r6ial0d]

could you explain again what is the problem.


M
Replied on 10/03/2016

here is the piece of my code, there is an error "Exception in thread "main" java.lang.NumberFormatException: For input string: "(12)". The fetched text from the link contains (12) how to separate those brackets and just use the no.

code
[color=#FF0000:291mdv05] WebDriver Findsum= new HtmlUnitDriver();
Findsum.manage().window().maximize();
Findsum.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

Findsum.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
List<WebElement> box = Findsum.findElements(By.xpath("//*[@id='secondary']/div[1]/div[3]/div/ul/li"));
System.out.println(box.size());

String link1="//*[@id='secondary']/div[1]/div[3]/div/ul/li[";
String link2="]/a/span[2]";

int sum =0;

for(int j =0; j<box.size(); j++)
{
List<WebElement> linksno = Findsum.findElements(By.xpath(link1+j+link2));
linksno.size();

for(int i = 0; i<linksno.size(); i++)
{
String text = linksno.get(i).getText();
int no = Integer.parseInt(text);

sum = sum + no;
}
}

System.out.println(sum);

String totaltext = Findsum.findElement(By.xpath("//*[@id='primary']/div[2]/div[1]/div[1]/span")).getText();
int totalno = Integer.parseInt(totaltext);

if(totalno == sum)
{
System.out.println("The brand list item are equal to total item");
}
else
{
System.out.println("Error");
}


}[/color:291mdv05]


M
Replied on 12/03/2016

you have to use substring function.

http://stackoverflow.com/questions/4503656/in-java-removing-first-character-of-a-string


M
Replied on 12/03/2016

Responsive image thanks for the link got the solutions