webdriver 处理富文本框疑问
初学selenium,遇到往富文本框写入内容的疑问,在浏览器console中写入文本能成功,程序中报错写入代码为String s = "document.getElementById('ueditor_0').contentWindow.document.body.innerText='test';";
((JavascriptExecutor)driver).executeScript(s);
报错信息为
debug1org.openqa.selenium.WebDriverException: document.getElementById(...) is null
Command duration or timeout: 24 milliseconds
Build info: version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:18:15'
System info: host: 'USER-20140421LD', ip: '192.168.1.122', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_05'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=30.0, platform=XP, browserConnectionEnabled=true, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 5dfa34b2-2b5f-400a-b498-94809f546e85
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:193)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
at org.openqa.selenium.remote.RemoteWebDriver.executeScript(RemoteWebDriver.java:504)
at com.x2.Todo.testAddTodo(Todo.java:90)
at com.x2.Todo.main(Todo.java:39)
浏览器console能写进内容
document.getElementById('ueditor_0').contentWindow.document.body.innerText='test';
"test"
请把问题写的明确点~~ 回复 2# lsekfe
是哪个地方不明确呢?用JS往富文本框写内容,JS代码:
document.getElementById('ueditor_0').contentWindow.document.body.innerText='test';在浏览器console中能写进去,
但是代码中:
String s = "document.getElementById('ueditor_0').contentWindow.document.body.innerText='test';";
((JavascriptExecutor)driver).executeScript(s);报错 学习了,谢谢楼主分享 需要选定 iframe driver.switchTo().frame(XXX)方法 回复 5# jia8162
选定iframe啥意思呢,就是进入iframe吗,这个肯定是进入了的driver.switchTo().frame("ueditor_0"); 纠结了这么久的问题,其实原因是刚刚开始已经进入iframe了,driver.switchTo().frame("ueditor_0");js里面又进入了一次,导致后面报contentWindow为空,js直接往body写内容就是了((JavascriptExecutor)driver).executeScript("document.body.innerText='spanish';");
都怪自己对js不熟悉,根本想不到,让开发看,一会儿就找到问题了
页:
[1]