在使用Serenity框架从PhantomJS驱动程序执行javascript时,我遇到了以下问题。
(PhantomJSDriver) driver).executeScript("document.getElementsById('whereOut')")
错误:
{"errorMessage":"'undefined' 不是一个函数(评估 'document.getElementsById('whereOut')')","request":{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-alive","Content-Length":"69","Content-Type":"application/json; charset=utf-8","Host":"localhost:33819","User-Agent":"Apache-HttpClient/4.3 (爪哇 1.5)"},"httpVersion":"1.1","method":"POST","post":"{\"script\":\"document.getElementsById(\u0027whereOut\u0027)\",\"args\":[]}","url":"/execute","urlParsed":{"anchor":","query":","file":"execute","directory":"/","path":"/execute","relative":"/execute","port":","host":","password":","user":","userInfo":","authority":","protocol":","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/session/b0246730-4435-11e6-b64c-7bf1e13a7577/execute"}} 命令持续时间或超时:278 毫秒
还尝试了以下代码:
((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut')")
上面的代码即使我添加返回值,也会返回一个 null 值,如下所示:
((JavascriptExecutor) driver).executeScript("return document.activeElement")
返回一个空值。
有没有人尝试使用phantom js更改特定元素的css
?
((JavascriptExecutor) driver).executeScript("document.getElementById('whereOut').className=''")
我发现的是,在执行上面的代码行时,我没有使用javascript
执行来获取元素,而可以使用纯硒代码和 Firefox 浏览器检索相同的元素。
附言我正在使用phantomJS 1.9.7版本以及宁静:1.0.56。
尝试将executeAsyncScript
与callback
一起使用,如下所示:-
String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];var element = document.getElementById('whereOut');element.setAttribute('class', 'your new class name');callback(element.getAttribute('class'));");
已编辑:- 如果您想修改欲望元素className
请尝试使用.setAttribute
WebElement element = driver.findElement(By.id("whereOut"));
String changedClassName = ((JavascriptExecutor) driver).executeAsyncScript("var callback = arguments[arguments.length - 1];arguments[0].setAttribute('class', arguments[1]);callback(arguments[0].getAttribute('class'));", element, "your new class name");
希望对您有所帮助:)