Selenium Python bindings: how to execute JavaScript on an element?

前端 未结 2 1042
予麋鹿
予麋鹿 2021-01-12 01:46

Have used python selenium script to trigger selenium server to run JavaScript code. It works fine.

drv.execute_script(\'\')
2条回答
  •  被撕碎了的回忆
    2021-01-12 02:24

    Thanks to the answer by @Richard who led me in the right direction and Brian's link (even thought it's for java) who helped me to figure out the meaning of "arguments".

    The following code will do what I need.

    ele = get_element_by_xpath('//button[@id="xyzw"]');
    drv.execute_script('arguments[0].setAttribute("style", "color: yellow; border: 2px solid yellow;")', ele)
    

提交回复
热议问题