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

前端 未结 2 1043
予麋鹿
予麋鹿 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:18

    execute_script accepts arguments, so you can pass the element:

    drv.execute_script('arguments[0].setAttribute("style", "color: yellow; border: 2px solid yellow;")', ele)
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题