How to convert a found IWebElement element to double type?

Code example:

 IWebElement SearchInput = Browser.FindElement(By.Id("betSize")); double G = Convert.ToDouble(SearchInput); 

Here I was answered a question, thank you. But then I had a new question. How to convert the number taken from the tag argument.

for example

 <input name="" value="0.00000000" autocomplete="off" id="betSize" type="text"> 

and if you try to convert it that way

 double G = Convert.ToDouble(SearchInput.Text); 

then get an error.

Decision

 double number; double.TryParse(SearchInput.Text, out number); 
  • Regarding the changes you made - I do not think that it is appropriate. My answer reveals all the nuances, including the conversion and receiving text from the element. The answer was an error with the data type that @Grundy pointed out to me, which I immediately corrected. As a result, you brought a solution - proposed by me. - Denis Bubnov

1 answer 1

You need to get the text from the found element, and then convert the value. Try this:

 var searchInput = Browser.FindElement(By.Id("betSize")).Text; double number; if (Double.TryParse(searchInput, out number)) { // number - успешно преобразованное значение } else { // что-то пошло не так и преобразовать не удалось } 

If your betSize element has a value that can be converted to double , the operation will work successfully.


From the Selenium WebDriver documentation - Selenium Documentation , retrieving text values ​​(C #):

Getting text values
People often wish to retrieve the innerText value contained within an element. This returns a single string value. The text displayed on the page.

 IWebElement element = driver.findElement(By.id("elementID")); element.Text; 

Conversion to double type:

Double.TryParse - method - Converts a string representation of a number to its equivalent double precision floating point number. Returns a value indicating whether the conversion was successful.

I do not think it would be superfluous to check whether the conversion was successful. Or use the Convert.ToDouble method , as written in you:

 double G = Convert.ToDouble(searchInput); 
  • Does .Text return an IWebElement ? - Grundy
  • @Grundy, yes you are right, I’ll edit now, I forgot. Returns the string - Denis Bubnov