gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:24:55 27 4
gpt4 key购买 nike

当我执行以下命令来更改 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com