2016-08-24 68 views
0

我試圖禁用文本框,但它無法按預期工作。下面是我的代碼如何在Selenium中禁用文本框

禁用命令:

JavascriptExecutor js = (JavascriptExecutor)driver; 
WebElement textbox = driver.findElement(By.id("textbox")); 
js.executeScript("arguments[0].setAttribute('disabled', '');", textbox); 

測試命令:

js.executeScript("arguments[0].setAttribute('value', 'Text Box Enalbled !! ');", textbox); 
+1

是什麼類型'js'對象? –

+1

@Rudziankoŭ它是一個'JavascriptExecutor' –

回答

1

For disable text box,你需要設置,如下textboxtrue.disabled屬性: -

JavascriptExecutor js = (JavascriptExecutor)driver; 
WebElement textbox = driver.findElement(By.id("textbox")); 
js.executeScript("arguments[0].disabled = true", textbox); 
+2

感謝這一個工作正常與我的測試代碼 –

1
JavascriptExecutor javascript = (JavascriptExecutor) driver; 
String todisable = "document.getElementsById('textbox')[0].setAttribute('disabled', '');"; 
javascript.executeScript(todisable); 
1

要禁用JavaScript中,你可以做的文本框:

JavascriptExecutor js = (JavascriptExecutor)driver; 
js.executeScript("document.getElementById('textbox').setAttribute('disabled', '');"); 
+2

,也工作,謝謝 –