Selenium Python绑定:如何在元素上执行JavaScript? [英] Selenium Python bindings: how to execute JavaScript on an element?
本文介绍了Selenium Python绑定:如何在元素上执行JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用python selenium脚本触发selenium服务器运行JavaScript代码。它工作正常。
Have used python selenium script to trigger selenium server to run JavaScript code. It works fine.
drv.execute_script('<some js code>')
但是,我无法弄清楚如何在使用get_element_by _ *()api检索的元素上运行javascript代码。例如,我
However, I can't figure out how to run javascript code on an element that was retrieved using get_element_by_*() api. For example, I
ele = get_element_by_xpath('//button[@id="xyzw"]');
#question: how do I change the "style" attribute of the button element?
如果我在浏览器的开发者控制台上,我可以将其作为
If I were on developer console of the browser, I can run it as
ele = $x('//button[@id="xyzw"]')[0]
ele.setAttribute("style", "color: yellow; border: 2px solid yellow;")
只是不知道如何在python selenium脚本中执行此操作。
提前致谢。
Just don't know how to do it in python selenium script. Thanks in advance.
推荐答案
execute_script
接受参数,因此您可以传递元素: / p>
execute_script
accepts arguments, so you can pass the element:
drv.execute_script('arguments[0].setAttribute("style", "color: yellow; border: 2px solid yellow;")', ele)
这篇关于Selenium Python绑定:如何在元素上执行JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文