0

I need to verify a value which is in "Unicef material-ui currency textfield". I use bellow code to get the value within the textbox but the value is empty.

var value = driver.FindElement(By.Id("txtCurrency")).Text;

If you inspect the input field in bellow sample, you can see even though text is visually displayed, "value" HTML attribute is empty.

HTML snapshot:

enter image description here

HTML:

<input aria-invalid="false" type="text" class="MuiInputBase-input MuiOutlinedInput-input jss493 jss561 MuiInputBase-inputAdornedStart MuiOutlinedInput-inputAdornedStart" value="">

Sample URL as bellow : https://unicef.github.io/material-ui-currency-textfield/#currencytextfield I would like to know a way to get the value/text of input textbox.

Project is in ReactJs and Selenium is in C#

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
shalinds
  • 63
  • 2
  • 8

2 Answers2

2

To get the value from input field you need to use .GetAttribute("value")

Use following xpath to identify the element.

var Value =driver.FindElement(By.XPath("//h6[text()='Outlined']/following::input[1]")).GetAttribute("value");
Console.WriteLine(Value);

Or following css selector

var Value =driver.FindElement(By.CssSelector(".MuiInputBase-input.MuiOutlinedInput-input.jss134.jss202.MuiInputBase-inputAdornedStart.MuiOutlinedInput-inputAdornedStart")).GetAttribute("value");
Console.WriteLine(Value);
KunduK
  • 32,888
  • 5
  • 17
  • 41
  • but @KunduK 'value' is empty in the HTML code when check from browser inspector element view. GetAttribute("value") won't work since there's no value for 'HTML value' attribute ? – shalinds Dec 10 '20 at 16:12
  • @shalinds : The value is generated by java scripts that's the reason it is empty. Copy my code and run it and let me know how this goes.. – KunduK Dec 10 '20 at 16:15
1

The desired element is a ReactJS enabled element so you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:

  • XPath and GetAttribute("value"):

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//h6[text()='Outlined']//following::div//input[contains(@class, 'MuiInputBase-input')]"))).GetAttribute("value"));
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352