0

I need to read input value of an element and save it to a string variable in my c# windows form application. Is it possible? Here is how I find the element:

IWebElement SearchCounter = Browser.FindElement(By.Id("counter")); 

and here is the html code of the element:

<div id="page-content"> 
    <input type="hidden" id="counter" value="355">

If it's not possible to get the value, how can I read this element html source and assign it to a string variable in my app?

I don't get any result using this:

string cs = SearchCounter.GetAttribute("innerHtml");                

2 Answers 2

1

If you want to get input value - you should use

string cs = SearchCounter.GetAttribute("value"); instead of GetAttribute("innerHTML");

If you want to retrieve html source of an element, use innerHtml.

Browser.FindElement(By.Id("page-content").GetAttribute("innerHTML") would return <input type="hidden" id="counter" value="355"> and SearchCounter.GetAttribute("innerHTML") would return something that's inside input.

Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

I will try this method too Thank you
0

To read the value of the value attribute, GetAttribute should be what you're looking for. For example, to get "Norway" out of that demo page, you'd use the following code: https://www.w3schools.com/Tags/tryit.asp?filename=tryhtml5_input_type_hidden

driver.Navigate().GoToUrl("https://www.w3schools.com/Tags/tryit.asp?filename=tryhtml5_input_type_hidden");
driver.SwitchTo().Frame(driver.FindElement(By.Id("iframeResult")));
var element = driver.FindElement(By.Name("country"));

var value = element.GetAttribute("value");
Console.WriteLine(value);

To your second question, the driver.PageSource property gives you the HTML of the current page. You would have to process that yourself then, of course.

var source = driver.PageSource;
Console.WriteLine(source);

1 Comment

you're welcome! if you'd accept/upvote the answers that were helpful for your, that would be nice :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.