在Selenium中获取Javascript代码的返回值 [英] Getting the return value of Javascript code in Selenium
本文介绍了在Selenium中获取Javascript代码的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 Selenium2 对我的网站进行一些自动化测试,我希望能够获得一些 Javascript 代码的返回值.如果我的网页中有一个 foobar()
Javascript 函数,并且我想调用它并将返回值放入我的 Python 代码中,我可以调用什么来执行此操作?
解决方案
要返回一个值,只需在传递给 execute_script()
return JavaScript 关键字> 方法,例如
I'm using Selenium2 for some automated tests of my website, and I'd like to be able to get the return value of some Javascript code. If I have a foobar()
Javascript function in my webpage and I want to call that and get the return value into my Python code, what can I call to do that?
解决方案
To return a value, simply use the return
JavaScript keyword in the string passed to the execute_script()
method, e.g.
>>> from selenium import webdriver
>>> wd = webdriver.Firefox()
>>> wd.get("http://localhost/foo/bar")
>>> wd.execute_script("return 5")
5
>>> wd.execute_script("return true")
True
>>> wd.execute_script("return {foo: 'bar'}")
{u'foo': u'bar'}
>>> wd.execute_script("return foobar()")
u'eli'
这篇关于在Selenium中获取Javascript代码的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文