我尝试获取一些网站属性(单元格的颜色)并在 Selenium 中进行比较。
当我把这个:
javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color");
在 Chrome Omnibox 中,我收到了正确的答案,但是当我使用 storeEval 或 assertEval 尝试获取此值时,它无法正常工作。
编辑:
我像这样使用 selenium 命令。我使用 storeEval,当我回显该值时,它会返回此命令。我使用火狐。我使用 Chrome 只是为了检查命令是否正确。 (应该是 "rgb(220, 22, 92)")
编辑2:
是的,命令没问题,但我在 Selenium-IDE 工具中使用它时遇到问题。当我将它与 storeEval 命令一起使用时,它不会返回值。
日志:
[信息] 脚本是:var test javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color");回声测试;
[信息] 执行:|echo | ${测试} | |
[信息] echo: var test javascript:window.getComputedStyle(document.getElementById("simple_cname"),null).getPropertyValue("background-color");回声测试;
我像这样输入 selenium 命令。我使用 storeEval,当我回显该值时,它会返回此命令。 我使用火狐。我使用 Chrome 只是为了检查命令是否正确。 (应该是 "rgb(220, 22, 92)")
最佳答案
您需要删除javascript:
部分并将document
引用为window.document
。该命令将如下所示:
window.getComputedStyle(window.document.getElementById('simple_cname'),null).getPropertyValue('background-color');
javascript:
部分仅在从您的 URL 栏运行代码时才需要,在其他地方任何地方 都是多余的。使用 Console
而不是你的多功能框在 Chrome 中运行 JS 命令。
document
-> window.document
东西在 the docs 中提到了在 storeEval
部分下。
另请注意,您的脚本只能在现代浏览器中运行,在 IE < 9 中会失败。如果您同意,那很好。如果没有,Google has the solution .
关于javascript - 通过CSS获取css值并在Selenium中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11227685/