python selenium执行js语句时返回为空?

最近写刷课的爬虫练手的时候遇到了一个问题:selenium执行js的时候返回值为空,但是在chrome的console中执行相同的语句的时候就能得到想要的结果,求大佬解答!

python代码:

script_coureName = """window.document.getElementById("iframe").contentWindow.document.querySelector('#ext-gen1040 > span').innerText;"""
courseName = self.browser.execute_script(script_coureName)
print("course:", courseName)

执行结果:
图片描述

chrome的console中执行结果:
图片描述

阅读 5.1k
2 个回答

脚本中需要return
console中:

>typeof(jQuery)
"function"

selenium中:

js = 'return typeof(jQuery)'
r = driver.execute_script(js)
print(r) #function
新手上路,请多包涵

我用的java写的 不行呀

clipboard.png
控制台输出这个

clipboard.png

推荐问题