0

Scenario: I am using Selenium Java. I need to add items to the cart and calculate the total price of the items in the cart.

Url: https://rahulshettyacademy.com/seleniumPractise/#/

enter image description here

Issue: My code is giving the following error

Exception in thread "main" java.lang.NumberFormatException: For input string: ""

My code:

String[ ] ItemsNeeded = {"Cucumber","Brocolli","Beetroot" };

addItemsCart (driver,ItemsNeeded);


public static void addItemsCart(WebDriver driver, String [] ItemsNeeded) {

List<WebElement> products = driver.findElements(By.cssSelector("h4.product-name"));

List<String> ItemsNeededList = java.util.Arrays.asList(ItemsNeeded); 

int j = 0;

int CalcTotalPrice = 0;

for (int i=0;i<products.size();i++)         

{

String[] name2 = products.get(i).getText().split("-");

String name3 = name2[0].trim(); 

   if (ItemsNeededList.contains(name3)) 

  {                 

      driver.findElements(By.xpath("//div[@class='product-action']/button")).get(i).click();

System.out.println("Added " + name3 + " to the cart");

String Price1 =driver.findElements(By.xpath("//p[@class='product-price']")).get(i).getText();

int IndPrice1 = Integer.parseInt(Price1);

CalcTotalPrice = CalcTotalPrice + IndPrice1 ;

System.out.println ("Calculated Total Price " +CalcTotalPrice);                 

j++;

       if (j==ItemsNeededList.size())
      {
       break;
      }
    }
}

Xml of one product:

enter image description here

Can anyone help me in resolving this issue?

2

This stack overflow question will help you in knowing how to debug an error:

https://sqa.stackexchange.com/a/43047/40022

The below question helps in understanding difference between gettext and getattribute:

How to get text from an element , when getText fails

So, in the stack trace, I had

at testsuites.test.package.login(test.java:94)

so it meant that the error was at line 94 in my test.java file. I checked the line and the code was:

 int IndPrice1 = Integer.parseInt(Price1);

When I debugged Price1 was returning an empty string, and that's why parsing is failing.

So, I checked how you are getting the text, and it was through below locator:

 String Price1 =driver.findElements(By.xpath("//p[@class='product-price']")).get(i).getText();

When I tried, //p[@class='product-price'] in chrome inspect, it was observed that the first element is not actually the first product's displayed price element. But the element that is present in the cart, so the more the items you add the more the element count increases.

So if you add Brinjal - 1 Kg, then the first element found using that locator is not the price displayed for Brocolli, but the price inside the cart, which is brinjal and is not displayed. So getText() won't work

so getText() fails as getText() retrieves only displayed texts. Instead, use:

 String Price1 =driver.findElements(By.xpath("//p[@class='product-price']")).get(i).getAttribute("textContent");

Also, you will always get 120 because your i value is 0,2,3. But it should be 0,1,2 as the elements is added in the order.

Output:

enter image description here

Recommendation:

recommendation always refer the parent element:

so for products: `div.product`
for price `products.get(i).findelement(p.product-price)`
for name `products.get(i).findelement(h4.product-name)`
for action `products.get(i).findelement(button)`
| improve this answer | |
  • PDHide...It was just brilliant. What a fantastic analysis. Reading your answers/solutions is itself an education. You are an asset not only to this website but for all selenium users. Your great observations and analytical skills have been motivating me to learn selenium more. Infectious effort. – SelfLearner Apr 10 at 22:32
  • You mentioned in your answer as - "Also, you will always get 120 because your i value is 0,2,3. But it should be 0,1,2 as the elements is added in the order.". But I added the first, third and fourth elements so the order should be 0,2 and 3 right? Why did you say as 0,1 and 2. Kindly clarify. – SelfLearner Apr 10 at 22:34
  • 1
    @SelfLearner try yourself manually , add each element and then try to get 0 ,2,3 from the website using chrome inspector, and see 1 ,3 and 4 , after adding things to cart – PDHide Apr 10 at 22:44
  • 1
    I have understood my mistake now. Please ignore my above comment. I am clear now. Thanks once again for solving the problem. – SelfLearner Apr 11 at 2:20
  • 1
    @SelfLearner you are not using .// , When using xpath use .// , If using css it works fine with normal locator you don't have to provide . – PDHide Apr 11 at 8:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.