繁体   English   中英

如何将jQuery与硒execute_script方法一起使用?

[英]How can I use jQuery with selenium execute_script method?

如果当前页面尚未使用jQuery,如何将jQuery与selenium execute_script方法execute_script使用?

例如:

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

我试过添加这样的脚本:

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

但是我收到错误消息,说jquery_script变量未定义。

加载本地jquery似乎更好:

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

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

它更快,您不必担心时序问题。

您有两个错误:

  • getElementsByTagName代替findElementsByTag
  • 您必须将其放在三元""" """或者必须将Javascript放在一行中。

在此之后,它添加了jQuery但使用$需要几行

第一:加载jQuery需要一些时间,因此需要time.sleep()

第二:此代码不会自动创建$ ,而是需要$ = window.jQuery;

from selenium import webdriver
import time

url = 'https://stackoverflow.com/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.com/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>")')

您也可以在第一个脚本中使用jquery_script.onload来运行将创建$代码

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

但是在使用$之前它仍然需要time.sleep()

我从Java加载jQuery并使用jQuery

from selenium import webdriver
import time

url = 'https://stackoverflow.com/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.com/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>")')

最终,您可以在onload全部运行,然后就不需要时间了time.sleep()

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

完整代码

from selenium import webdriver
import time

url = 'https://stackoverflow.com/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.com/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);""")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM