【发布时间】:2014-10-11 16:13:18
【问题描述】:
已使用 python selenium 脚本触发 selenium 服务器运行 JavaScript 代码。它工作正常。
drv.execute_script('<some js code>')
但是,我不知道如何在使用 get_element_by_*() api 检索的元素上运行 javascript 代码。例如,我
ele = get_element_by_xpath('//button[@id="xyzw"]');
#question: how do I change the "style" attribute of the button element?
如果我在浏览器的开发者控制台上,我可以运行它
ele = $x('//button[@id="xyzw"]')[0]
ele.setAttribute("style", "color: yellow; border: 2px solid yellow;")
只是不知道如何在 python selenium 脚本中执行此操作。 提前致谢。
【问题讨论】:
-
看看
Selenium中的JavascriptExecutor接口。 -
感谢@Brian 提供链接。它用于 java 绑定,但是,它让我理解了工作解决方案中的“参数”“...arguments[0].setAttribute(....)”的含义。 javascript使用它来引用函数参数(尤其是当函数的参数数量可变时)。
-
完全没问题。我喜欢分享知识。
标签: javascript python selenium selenium-webdriver