-
Notifications
You must be signed in to change notification settings - Fork 1.4k
-
Hi !
I tried to insert a script element in the page with the fucntion execute_script but nothing happens ! My element is not created.
sb.execute_script("var myscript = document.createElement('script');myscript.appendChild(document.createTextNode('//Test'));document.body.append(myscript);") sb.activate_cdp_mode(current_url)
i also tried with cdp.reload and script_to_evaluate_on_load but same result !
sb.cdp.reload(script_to_evaluate_on_load="var myscript = document.createElement('script');myscript.appendChild(document.createTextNode('//Test'));document.body.append(myscript);")
Can you help me ?
Thanks
Beta Was this translation helpful? Give feedback.
All reactions
Would be similar to how a button is added, as in #1429 (comment):
from seleniumbase import SB with SB() as sb: script_to_add_button = """function injectButton() { var new_button=document.createElement('button'); document.getElementsByTagName('body')[0].appendChild(new_button); new_button.style="width: 200px; height: 120px"; } injectButton();""" sb.execute_script(script_to_add_button) sb.sleep(3)
Replies: 2 comments 3 replies
-
Would be similar to how a button is added, as in #1429 (comment):
from seleniumbase import SB with SB() as sb: script_to_add_button = """function injectButton() { var new_button=document.createElement('button'); document.getElementsByTagName('body')[0].appendChild(new_button); new_button.style="width: 200px; height: 120px"; } injectButton();""" sb.execute_script(script_to_add_button) sb.sleep(3)
Beta Was this translation helpful? Give feedback.
All reactions
-
Thanks !
I will try this solution.
If the url is redirected, will the button disappear?
Beta Was this translation helpful? Give feedback.
All reactions
-
The button (and the rest of the page) is gone if there's a redirect after you call that.
Beta Was this translation helpful? Give feedback.
All reactions
-
And can i do the same thing with parameter script_to_evaluate_on_load for the reload function ?
Beta Was this translation helpful? Give feedback.
All reactions
-
That’s one of the options. You can try that.
Beta Was this translation helpful? Give feedback.