import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from time import sleep
from selenium import webdriver
import json
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://example.com/')
body=driver.find_element_by_tag_name('body')
driver.execute_script('''var script = document.createElement("script");
var button = document.createElement("button")
button.setAttribute("type", "button");
button.setAttribute("id", "seleniumbutton");
button.setAttribute("onClick", "myFunction()");
button.textContent="Click me to continue selenium";
script.setAttribute("id", "seleniumscript");
script.textContent=`function myFunction() {
alert("You pressed a key inside the input field");
document.querySelector("#seleniumbutton").remove();
document.querySelector("#seleniumscript").remove();
}`;
arguments[0].appendChild(button);
arguments[0].appendChild(script);
''',body)
WebDriverWait(driver,1000000000000000).until(EC.alert_is_present())
driver.switch_to_alert().accept()
input("Completed")
you can add an alert and button to the website and then wait for it . See the example. Here unless you click the button it won't execute your script