Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FindElement does not return element text unless actually in DOM

I'm using Selenium's webdriver, but I've hit upon a problem. I'm using KnockoutJS to bind my UI to data from the server. In my tests when I call FindElement(By.Id("InputField")) it is returning the element ok, but unfortunately the Text() field is empty.

When I run a Jquery selector on the field:

$("#InputField").val() it gives me the text in the input field ok.

When I inspect the Html source, there is no value in the input field, and I guess this is because KnockoutJS is binding the value to the input field late.

How do I get WebDriver to pull the attributes, text etc. from the field correctly rather than just empty text?

like image 398
jaffa Avatar asked Mar 02 '26 23:03

jaffa


1 Answers

Instead of using the Text property try using element.GetAttribute("value")

IWebElement element = _driver.FindElementById("InputField");
string value = element.GetAttribute("value");
like image 127
RP Niemeyer Avatar answered Mar 05 '26 20:03

RP Niemeyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!