[英]How to do sum of the numbers displayed on webpage in selenium webdriver with java
My Scenario 我的场景
"Go to: http://www.americangolf.co.uk/golf-clubs/fairway-woods " “转到: 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" 找到的产品总数写在页面“ 49找到的产品”上
Verify sum s equal to number of products mentioned 验证总和s是否等于提到的产品数量
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. 在上面的代码中,我增强了xpath以便最小化UI更改的影响。 Let me know, if you have any further queries.
让我知道,如果您还有其他疑问。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.