2016-11-28 104 views
0

我遇到了Firefox輸入文本框中的文本(在彈出框內)的情況,這是一個必填字段。當我點擊按鈕時,它會顯示警告標誌。當我檢查HTML時,它顯示文本框中沒有值,並且需要HTML顯示字段中的titleFirefox無法識別它在文本框中輸入的文本

我嘗試使用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> 

Image for reference

有人可以幫我一下吧。

版本: 火狐 - 50.0 硒3.0.0

感謝。

回答

0

嘗試如下:

driver.findElement(By.Id("newName")).sendKeys(text); 
+0

錯誤的'C#'語法在這裏......應該是FindElement和By.Id' –

+0

@PseudoSudo,謝謝你的通知。習慣了Java語法。 –

+0

這沒有奏效。它在文本框中輸入文本,但是當我點擊按鈕時,它顯示強制性警告消息。如果您參考了圖片,您可以看到HTML。在我發佈的代碼中,我已經使用了sendkeys。我用'名字'和'身份證'也嘗試過。這裏的問題是雖然使用sendKeys輸入文本,但它不存儲該值並認爲它是空的。 – Sudeepthi

0

試試這個:

var username = "User Name Here"; 
driver.FindElement(By.Id("newName")).sendKeys(username); 
+0

它沒有工作。請參閱我的評論。硒進入文本,但當我點擊按鈕時,它需要導航到另一個頁面,而不是顯示必填字段錯誤標誌。 – Sudeepthi

0

我會切換到ChromeDriver現在。我不認爲webdriver 3.0在Firefox v50上運行良好。如果您必須使用Firefox,請下載一個版本並轉到以前版本的Webdriver。

+0

只有2個我遇到問題的測試用例(有些問題可以通過解決某些問題來解決)。我已經在IE和Chrome中運行。 Opera停止使用最新版本,所以我不得不停止在Opera中運行。我也無法阻止FF。你能告訴我哪個版本的FF與最新的硒兼容 – Sudeepthi