javascript - 通过CSS获取css值并在Selenium中使用

标签 javascript css testing selenium selenium-ide

我尝试获取一些网站属性(单元格的颜色)并在 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/

相关文章:

javascript - 如果单词为空格,则将下一个单词换行

javascript - Bootstrap 工具提示与实际示例工具提示按钮的用法?

javascript - 胖箭头函数的范围和 jQuery 回调

javascript - Jquery,悬停时改变颜色

django - 如何为 Django 项目构建功能测试

javascript - 在 NodeJS 中使用 BETWEEN 子句

html - 如何在 Xaringan 演示文稿中调整图像大小?

unit-testing - 参数化 Spock 设置

spring - org.springframework.test.web.server.samples.context.SecurityRequestPostProcessors 的 Maven 依赖

css - Flexbox 未对齐