0

I got this error when I am converting the String to double, I want that value in Double data type

This is my code

WebElement Rm_rate = driver.findElement(By.xpath("//div[@class='MT10']//input[@value= '4424']/following::input[@name = 'avgDiscountRate1']"));
String  r1_rate = Rm_rate.getAttribute("value");
room_rate = Double.parseDouble(r1_rate); //

The wave element is

<input type="hidden" value="1000.00" name="avgDiscountRate1">
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

1 Answers1

0

You were close enough. You need to initialize the variable room_rate first.

WebElement Rm_rate = driver.findElement(By.xpath("//div[@class='MT10']//input[@value= '4424']/following::input[@name = 'avgDiscountRate1']"));
String  r1_rate = Rm_rate.getAttribute("value");
double room_rate = Double.parseDouble(r1_rate);

In a single line:

double room_rate = Double.parseDouble(driver.findElement(By.xpath("//div[@class='MT10']//input[@value= '4424']/following::input[@name = 'avgDiscountRate1']")).getText());

However the WebElement seems to have the attribute type="hidden". So you need to removeAttribute() the type attribute and you can use the following solution:

WebElement Rm_rate = driver.findElement(By.xpath("//div[@class='MT10']//input[@value= '4424']/following::input[@name = 'avgDiscountRate1']"));
((JavascriptExecutor)driver).executeScript("arguments[0].removeAttribute('type')", Rm_rate);
double room_rate = Double.parseDouble(driver.findElement(By.xpath("//div[@class='MT10']//input[@value= '4424']/following::input[@name = 'avgDiscountRate1']")).getText());
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352