我遇到的情况是Firefox在文本框中输入文本(在弹出窗口内),这是一个必填字段。当我点击按钮时,它会显示警告标志。当我检查HTML时,它显示文本框中没有值,并且HTML show字段中需要title
。
我尝试使用JavaScript输入文字,但没有帮助。
代码段:
public static void UserName(string text)
{
try
{
IJavaScriptExecutor js = (IJavaScriptExecutor)Drivers._driverInstance;
IWebElement element = Drivers._driverInstance.FindElement(By.Id("newName"));
//js.ExecuteScript("document.getElementById('newName').setAttribute('value', '" + text + "')");
js.ExecuteScript("arguments[0].click();", element);
js.ExecuteScript("document.getElementById('newName').value='" + text + "';");
//Drivers._driverInstance.FindElement(By.Name("newName")).Clear();
//Drivers._driverInstance.FindElement(By.Name("newName")).SendKeys(text);
}
catch(Exception e)
{
throw new Exception("Couldn't send text to username textbox " + e);
}
}
文本框的HTML:
<div class="field-container">
<label>Username</label>
<input id="newName" class="input-validation-error" name="newName" data-bind="value: Name" title="This field is required." data-orig-title="This field is required." type="text"/>
<span class="validationMessage" style="">This field is required.</span>
有人可以帮助我。
版本: 火狐-50.0 硒 - 3.0.0
感谢。
答案 0 :(得分:0)
尝试如下:
driver.findElement(By.Id("newName")).sendKeys(text);
答案 1 :(得分:0)
试试这个:
var username = "User Name Here";
driver.FindElement(By.Id("newName")).sendKeys(username);
答案 2 :(得分:0)
我现在要切换到ChromeDriver。我不认为webdriver 3.0在Firefox v50上运行良好。如果你必须使用firefox,请下载一个版本并转到以前版本的webdriver。