Error in Amazon program | Selenium Forum
R
Rajan Kumar Kaushik Posted on 20/02/2021

Hi Ashish,

I am doing practice of Amazon excercise as you given but the problem is when I replacing "?" and "," from the price string I am getting error.I have used replaceall function as well but in console I am getting error.Could you please help me on the same. I am pasting the code and attaching the screenshot of console.

Thanks in advance.

 

package excercise3;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import base.TestBase;

public class Amazon extends TestBase {

@Test
public void amazon() {
launchBrowser("Chrome");
driver.manage().window().maximize();
driver.get("https://www.amazon.in/");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys("iphone");
driver.findElement(By.id("twotabsearchtextbox")).sendKeys(Keys.ENTER);
List<WebElement> names=driver.findElements(By.xpath("//h2[@class='a-size-mini a-spacing-none a-color-base s-line-clamp-2']"));
List<WebElement>prices=driver.findElements(By.xpath("//span[@class='a-price']"));

System.out.println(names.size());
System.out.println(prices.size());

for(int i=0;i<names.size();i++) {
if(names.get(i).getText().contains("Apple iPhone 7 (32GB) - Black")) {
System.out.println(names.get(i).getText()+"--------"+prices.get(i).getText());

String p=prices.get(i).getText().replaceAll("[?,]", "");



int price=Integer.parseInt(p);
System.out.println(price);

}

}



}
}


A
Ashish Thakur Replied on 21/02/2021

Can you please post stack trace of error 


R
Rajan Kumar Kaushik Replied on 22/02/2021

[RemoteTestNG] detected TestNG version 7.0.0
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
ChromeDriver was started successfully.
Feb 22, 2021 10:27:18 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
18
18
Apple iPhone 7 (32GB) - Black--------?23,990
FAILED: amazon
java.lang.NumberFormatException: For input string: "?23990"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at excercise3.Amazon.amazon(Amazon.java:35)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
===============================================

 


A
Ashish Thakur Replied on 23/02/2021

java.lang.NumberFormatException: For input string: "?23990"

 

There is a question mark in number string. Thats Why


R
Rajan Kumar Kaushik Replied on 25/02/2021

Thanks


Related Posts