0

Parsing is not working for me - After getting 'newSocket' as a String with getText(), I'm trying to parse it into a double , and I get an exception error after running the test. Please help

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class SeleniumPractice2investing {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "C:\\automation\\drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://www.investing.com"); 



        driver.navigate().refresh();
        WebElement e1 = driver.findElement(By.id("searchTextTop")); 
        e1.sendKeys("BTC/USD");
        Thread.sleep(5000); // Wait until it is loaded
        WebElement e2 = driver.findElement(By.id("symbol_BTC/USD"));
        e2.click();
        Thread.sleep(5000);

        WebElement e3 = driver.findElement(By.id("last_last"));
        String newSocket = e3.getText();
        Thread.sleep(1);


        double CurrentSocketValue = Double.valueOf(newSocket); // Getting Exception error here
dima edunov
  • 59
  • 1
  • 9
  • when adding a "test" command as such: System.out.println("Test" +newSocket); double CurrentSocketValue = Double.valueOf(newSocket); >I get a numeric value (As a string, so it is not an empty String) – dima edunov Nov 26 '17 at 16:49
  • I Think I've Got it ! As the number comes in a String with punctuation (I.e. 2,4544.5) Should I remove punctuation before using parsing, as such : String123 = newSocket.replaceAll("[\\W]", ""); ? – dima edunov Nov 26 '17 at 17:00

2 Answers2

0

What is the Exception message?

From what you have posted i can only guess that the e3.getText() is an empty string. You could check for an empty string and set newSocket a default value.

If it is a string then there are other methods you can use in this Answer.

0

Empty String it is. The reason is that the number is received as so "2,749.79" with punctuations, I found a solution to ignore punctuations and it solved it (added a comment above)

dima edunov
  • 59
  • 1
  • 9