使用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脚本中这样做。提前谢谢。
发布于 2014-08-19 01:14:14
execute_script接受参数,因此您可以传递元素:
drv.execute_script('arguments[0].setAttribute("style", "color: yellow; border: 2px solid yellow;")', ele)发布于 2014-08-19 03:33:04
感谢@Richard的回答,他把我引向了正确的方向,以及Brian的链接(尽管它是针对java的),他帮助我理解了“参数”的含义。
下面的代码将完成我所需要的工作。
ele = get_element_by_xpath('//button[@id="xyzw"]');
drv.execute_script('arguments[0].setAttribute("style", "color: yellow; border: 2px solid yellow;")', ele)https://stackoverflow.com/questions/25368547
复制相似问题