问题
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:
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#
回答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"));
回答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);
来源:https://stackoverflow.com/questions/65236631/selenium-get-value-from-unicef-material-ui-currency-textfield