My Scenario
"Go to: http://www.americangolf.co.uk/golf-clubs/fairway-woods "
Print every link under brand
Every link will have a number in front of it
Find sum of all numbers
Total number of products found is written on the page-"49 Products found"
Verify sum s equal to number of products mentioned
My Code
public class americangolf {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> elem = driver.findElements(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a"));
for(WebElement li:elem)
{
System.out.println(li.getText());
}
List<WebElement> elem1 = driver.findElements(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]"));
for(WebElement li1:elem1)
{
System.out.println(li1.getText());
}
int con= Integer.parseInt(driver.findElement(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]")).getText());
for(int i=0;i<elem1.size();i++)
{
int sum = i+con;
System.out.println("Total is:"+sum);
}
}
}
Console output
CobraGolf (10)
CallawayGolf (9)
TaylorMade (7)
Ping (6)
WilsonStaff (4)
NikeGolf (3)
Benross (2)
Titleist (2)
Wilson (2)
Fazer (1)
MizunoGolf (1)
USKidsGolf (1)
YONEX (1)
(10)
(9)
(7)
(6)
(4)
(3)
(2)
(2)
(2)
(1)
(1)
(1)
(1)
Exception in thread "main" java.lang.NumberFormatException: For input string: "(10)"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at module13.americangolf.main(americangolf.java:39)
I am getting the above error while doing the sum of all printed numbers. I don't know how to get rid of it. Any solutions to this?
You are getting the whole string (with the parentheses) to sum.
one solution (definitely not the best one) is to replace this:
int con= Integer.parseInt(driver.findElement(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]")).getText());
for this:
int con= Integer.parseInt(driver.findElement(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]")).getText().replace("(","").replace(")","");
By doing that, you'll be able to parse as an Integer
Try following:
public class americangolf {
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
List<WebElement> elem = driver.findElements(By.xpath("//div[@class='refinement brand']/descendant::a"));
for(WebElement li:elem)
{
System.out.println(li.getText());
}
List<WebElement> elem1 = driver.findElements(By.xpath("//div[@class='refinement brand']/descendant::a/span[2]"));
int sum = 0;
for(WebElement li1:elem1)
{
System.out.println(li1.getText());
String s = li1.getText().replace("(", "");
sum = sum + Integer.parseInt(s.replace(")", ""));
}
System.out.println("Total sum is: " + sum);
int totalAppearingOnPage = Integer.parseInt(driver.findElement(
By.xpath("//h1[normalize-space(text())='Best Selling Fairway woods']/following::div[@class='results-hits'][1]/span")).
getText());
System.out.println("Total appearing on page: " + totalAppearingOnPage);
driver.quit();
}
}
In the above code, I have enhanced the xpaths in order to minimize the impact of UI changes. Let me know, if you have any further queries.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.