如果当前页面尚未使用 jQuery,如何将 jQuery 与 selenium 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()')
速度更快,而且您不必担心时间问题。
关于javascript - 如何将 jQuery 与 seleniumexecute_script 方法一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57941221/