简体   繁体   English

如何使用Java对Selenium Webdriver中的网页上显示的数字求和

[英]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.

相关问题 如何在网页上使用带有 Java 的 Selenium WebDriver 验证工具提示文本 - How to verify tooltip text using Selenium WebDriver with java on a webpage 如何使用 Java 检查下拉列表中的选项在 Selenium Webdriver 中显示两次 - How to check that the options in dropdown are displayed twice in Selenium Webdriver using Java 如何使用 Java 设置 Selenium WebDriver? - How do I setup Selenium WebDriver with Java? 如何使用Selenium WebDriver随机单击按钮并在显示的输入框中插入数字 - How to use selenium webdriver to randomly click on a button and insert numbers to the input box displayed 如何使用 Selenium Webdriver 在网页中找到“光标”位置? - How to find “Cursor” position in webpage with Selenium Webdriver? Selenium Webdriver:如何验证是否已加载所需的网页 - Selenium Webdriver: How to verify that the required webpage is loaded 如何使用Selenium WebDriver配置网页语言 - How to configure webpage language using selenium webdriver Selenium WebDriver中不显示CSS - CSS is not displayed in Selenium WebDriver 如何在Java中使用Selenium WebDriver查找图像标签,以及如何检查它是否已正确显示? - how to find an image tag using selenium webdriver in java and how to check if it has been properly displayed? Selenium WebDriver - 使用Java - 如何检查网页中是否显示错误消息? - Selenium WebDriver - Using Java - How can I check if error messages are visible or not in a webpage?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM