最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I use jQuery with selenium execute_script method? - Stack Overflow

programmeradmin3浏览0评论

How can I use jQuery with the selenium execute_script method, if the current page isn't already using jQuery?

For example:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
driver.execute_script('$("#id").wrap("<h1></h1>")')

I've tried adding a script like this:

driver.execute_script(
"var jquery_script = document.createElement('script'); 
jquery_script.src = '.7.1/jquery.min.js';
document.findElementsByTag('head')[0].appendChild(jquery_script)"
)

But I get errors saying that the jquery_script variable is undefined.

How can I use jQuery with the selenium execute_script method, if the current page isn't already using jQuery?

For example:

from selenium import webdriver
driver = webdriver.Firefox()
driver.get(url)
driver.execute_script('$("#id").wrap("<h1></h1>")')

I've tried adding a script like this:

driver.execute_script(
"var jquery_script = document.createElement('script'); 
jquery_script.src = 'https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js';
document.findElementsByTag('head')[0].appendChild(jquery_script)"
)

But I get errors saying that the jquery_script variable is undefined.

Share Improve this question edited Sep 15, 2019 at 16:49 Lord Elrond asked Sep 15, 2019 at 4:44 Lord ElrondLord Elrond 16.1k8 gold badges54 silver badges91 bronze badges 4
  • 2 you could use javascript to insert <script src="http//.../path/to/jquery.js"></script> to load jQuery – furas Commented Sep 15, 2019 at 5:38
  • Load jQuery with Javascript and use jQuery – furas Commented Sep 15, 2019 at 5:42
  • @furas thanks, but I've already tried that and couldn't manage to get it to work. I added an update showing my attempt. – Lord Elrond Commented Sep 15, 2019 at 16:50
  • you have two mistakes: getElementsByTagName instead of findElementsByTag and you have to put it in triple """ """ or you have to put Javascript in one line. And then it runs correctly but still it doesn't create $ which you can use in next script. Using code from link (in answer marked as accepted) I could run my code with $ – furas Commented Sep 15, 2019 at 19:35
Add a ment  | 

2 Answers 2

Reset to default 8

It seems way better to load a local jquery:

with open('jquery.js', errors='ignore') as f:
  driver.execute_script(f.read())

title = driver.execute_script('return $("title").text()')

It's faster and you don't have to worry about timing issues.

You have two mistakes:

  • getElementsByTagName instead of findElementsByTag
  • you have to put it in triple """ """ or you have to put Javascript in one line.

After this it adds jQuery but it need few lines more to use $

First: it needs some time to load jQuery so it needs time.sleep()

Second: this code doesn't create automatically $ and it needs $ = window.jQuery;

from selenium import webdriver
import time

url = 'https://stackoverflow./questions/57941221/how-can-i-use-jquery-with-selenium-execute-script-method'
driver = webdriver.Firefox()
driver.get(url)

driver.execute_script("""var jquery_script = document.createElement('script'); 
jquery_script.src = 'https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js';
document.getElementsByTagName('head')[0].appendChild(jquery_script);""")

time.sleep(0.5) # time to load jQuery library
driver.execute_script('$ = window.jQuery;')

driver.execute_script('$("h1").wrap("<i></i>")')
#driver.execute_script('$ = window.jQuery;$("h1").wrap("<i></i>")')

You can also use jquery_script.onload in first script to run code which will create $

jquery_script.onload = function(){var $ = window.jQuery;};

but it still need time.sleep() before you use $.

I took this from Load jQuery with Javascript and use jQuery

from selenium import webdriver
import time

url = 'https://stackoverflow./questions/57941221/how-can-i-use-jquery-with-selenium-execute-script-method'
driver = webdriver.Firefox()
driver.get(url)

driver.execute_script("""var jquery_script = document.createElement('script'); 
jquery_script.src = 'https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js';
jquery_script.onload = function(){var $ = window.jQuery;};
document.getElementsByTagName('head')[0].appendChild(jquery_script);""")

time.sleep(0.5) # time to load jQuery library

driver.execute_script('$("h1").wrap("<i></i>")')

Eventually you can run all in onload and then you don't need time.sleep()

jquery_script.onload = function(){var $ = window.jQuery; $("h1").wrap("<i></i>");};

Full code

from selenium import webdriver
import time

url = 'https://stackoverflow./questions/57941221/how-can-i-use-jquery-with-selenium-execute-script-method'
driver = webdriver.Firefox()
driver.get(url)

driver.execute_script("""var jquery_script = document.createElement('script'); 
jquery_script.src = 'https://ajax.googleapis./ajax/libs/jquery/1.7.1/jquery.min.js';
// jquery_script.onload = function(){var $ = window.jQuery; $("h1").wrap("<i></i>");};
jquery_script.onload = function(){
  var $ = window.jQuery; 
  $("h1").wrap("<i></i>");
};
document.getElementsByTagName('head')[0].appendChild(jquery_script);""")
发布评论

评论列表(0)

  1. 暂无评论