javascript - 执行 javascript 更改 webelement 时找不到变量错误

标签 javascript python selenium selenium-webdriver

当我执行以下命令来更改 webelement 文本值时,出现“cant error variable : e”

sel=webdriver.PhantomJS()
sel.get=('http://stackoverflow.com/questions?pagesize=50&sort=newest')
elements=sel.find_elements_by_xpath('//a[@href="/questions/31762200/traversing-through-nsset-is-slower-than-traversing-nsarray"]')
e=elements[0]
sel.execute_script(" e.text='testing' ")


WebDriverException: Message: {"errorMessage":"Can't find variable: e","request":
{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"95","Content-Type":"application/
json;charset=UTF-8","Host":"127.0.0.1:38324","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\": \"255a60c0-384f-11e5-
b643-5d2e25d3f62d\", \"args\": [], \"script\": \"e.text='testing'\"}","url":"/execute","urlParsed":{"anchor":"","query":"","file":"execute","directory":"/","path":"/
execute","relative":"/execute","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/execute","queryKey":{},"chunks":["execute"]},"urlOriginal":"/
session/255a60c0-384f-11e5-b643-5d2e25d3f62d/execute"}}

屏幕截图:可通过屏幕获得

最佳答案

python 代码和 javascript 代码不共享变量。


通过将附加参数传递给 Webdriver.execute_script,将变量 e 从 Python 端传递给 javascript .在 javascript 代码中,您可以使用 arguments 数组访问参数:

sel.execute_script("arguments[0].text = 'testing'", e)

关于javascript - 执行 javascript 更改 webelement 时找不到变量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31769817/

相关文章:

python - 无法导入tensorflow(使用pycharm IDE)

python - 使用 pip 安装模块,未找到

java - 动态元素里面有几个 'li'

java - Selenium Webdriver JAVA - 无法从下拉菜单中选择选项(JAVA)

javascript - 如何升级Vue版本

javascript - 查询 :not selector doesn't work with style on class

javascript - 将路由参数传递给 ExpressJS 中的 Controller

python - 将元组列表转换为字符串 Python

python - 将 Selenium 与 Python 结合使用 : Error when using select boxes on IE9

javascript - jsx图点更新事件