【问题标题】:How to do sum of the numbers displayed on webpage in selenium webdriver with java如何用java对selenium webdriver中网页上显示的数字求和
【发布时间】:2017-02-02 15:00:40
【问题描述】:

我的情景

“转到: http://www.americangolf.co.uk/golf-clubs/fairway-woods"

打印品牌下的每个链接

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

求所有数的总和

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

验证 sum s 是否等于提及的产品数量

我的代码

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)

我在对所有打印数字求和时遇到上述错误。我不知道如何摆脱它。有什么解决办法吗?

【问题讨论】:

    标签: java selenium


    【解决方案1】:

    尝试以下操作:

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

    在上面的代码中,我增强了 xpaths 以尽量减少 UI 更改的影响。如果您还有任何疑问,请告诉我。

    【讨论】:

      【解决方案2】:

      您正在对整个字符串(带括号)求和。

      一个解决方案(绝对不是最好的)是替换这个:

      int con= Integer.parseInt(driver.findElement(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]")).getText());
      

      为此:

      int con= Integer.parseInt(driver.findElement(By.xpath("//div[@id='secondary']/div[1]/div[3]/div/ul/li/a/span[2]")).getText().replace("(","").replace(")","");
      

      通过这样做,您将能够解析为整数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-05-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多