如何使用java在selenium webdriver的网页上显示的数字总和(How to do sum of the numbers displayed on webpage in selenium

编程入门 行业动态 更新时间:2024-10-17 14:18:28
如何使用java在selenium webdriver的网页上显示的数字总和(How to do sum of the numbers displayed on webpage in selenium webdriver with java)

我的情景

“请访问: http : //www.americangolf.co.uk/golf-clubs/fairway-woods ”

打印品牌下的所有链接

每个链接都会在它前面有一个数字

找到所有数字的总和

找到的产品总数写在页面上 - “找到49个产品”

验证总和等于提到的产品数量

我的代码

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); } } }

控制台输出

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)

在执行所有打印数字的总和时出现上述错误。 我不知道如何摆脱它。 任何解决方案?

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?

最满意答案

试试以下内容

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(); } }

在上面的代码中,我已经增强了xpath以最大限度地减少UI更改的影响。 让我知道,如果你有任何进一步的疑问。

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.

更多推荐

本文发布于:2023-08-07 14:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:总和   如何使用   数字   网页   selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!