StuffHappens
StuffHappens

Reputation: 6557

Get input text by xPath

I try to write autotest using C# and Selenium framework, but I can't get text of an input field. The html of the element:

<input id="WorkPlacePageEmployerKPPcb96e63d-c837-4e7f-827b-2e7d49370f20TextEdit-el" class="base-edit-input ts-box-sizing" placeholder="Empty" value="" style="" tabindex="1" disabled="disabled" type="text">

In my browser I see that it has some text inside, but the following code

inputElement.Text

returns empty string. inputElement is of OpenQA.Selenium.IWebElement type.

What am I doing wrong?

Upvotes: 1

Views: 1405

Answers (2)

Abhishek Yadav
Abhishek Yadav

Reputation: 459

It seems visible text is 'Empty' on browser which is visible to you. Please use below code to fetch the text visible on browser:

inputElement.GetAttribute("placeholder")

Also you might use JavaScriptExceutor for this purpose as suggested by @Andersson:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string inputText = (string)js.ExecuteScript("return arguments[0].placeholder", inputElement);

Upvotes: 1

Andersson
Andersson

Reputation: 52665

<input type="text"> has no text content, but attribute "value" that changes after user send keys

Try to replace

inputElement.Text

with

inputElement.GetAttribute("value")

Also you might use JavaScriptExceutor for this purpose:

IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
string inputText = (string)js.ExecuteScript("return arguments[0].value", inputElement);

Upvotes: 1

Related Questions