获取Selenium中Javascript代码的返回值 [英] Getting the return value of Javascript code in Selenium

查看:116
本文介绍了获取Selenium中Javascript代码的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Selenium2对我的网站进行一些自动化测试,我希望能够获得一些Javascript代码的返回值。如果我在我的网页上有一个 foobar() Javascript函数,我想调用它并将返回值放入我的Python代码中,我可以调用什么呢? / p>

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?

推荐答案

要返回一个值,只需使用 return JavaScript关键字字符串传递给 execute_script()方法,例如

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆