Skip to content

Instantly share code, notes, and snippets.

@anhldbk
Last active April 15, 2023 03:53
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save anhldbk/dc7a040b7fda199a5791 to your computer and use it in GitHub Desktop.
Save anhldbk/dc7a040b7fda199a5791 to your computer and use it in GitHub Desktop.
Inject jQuery into Selenium Driver
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.facebook.com/')
with open('jquery-1.9.1.min.js', 'r') as jquery_js:
jquery = jquery_js.read() #read the jquery from a file
driver.execute_script(jquery) #active the jquery lib
driver.execute_script("$('#email').text('anhld')")
@mandadimuralidharreddy
Copy link

driver.execute_script("$('#email').text('anhld')")
should be
driver.execute_script("$('#email').val('anhld')")

@x011
Copy link

x011 commented Apr 15, 2020

For remote install:

import requests
from selenium import webdriver

driver = webdriver.Firefox()
driver.get("https://banned.video")
jquery = requests.get("https://code.jquery.com/jquery-1.12.4.min.js").text
driver.execute_script(jquery)
driver.execute_script("""
    if (window.jQuery) {  
        // jQuery is loaded  
        alert("Yeah!");
    } else {
        // jQuery is not loaded
        alert("Doesn't Work");
    }
""")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment