How to return a value when using execute_script in capybara?

一个人想着一个人 提交于 2019-12-01 14:56:19

The Poltergeist driver is designed to return nil for execute_script:

def execute_script(script)
  browser.execute(script)
  nil
end

Poltergeist will only return a value if you use the evaluate_script:

def evaluate_script(script)
  browser.evaluate(script)
end

Capybara has corresponding methods for each - ie Session#execute_script and Session#evaluate_script. Your code should work if you switch to using evaluate_script (and as @AndreyBotalov points out, you also need to remove the return):

puts page.evaluate_script("somefunction();").to_i.inspect
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!