ruby变量在driver.execute_script之后返回nil

时间:2014-02-11 20:32:14

标签: javascript jquery ruby ajax selenium-webdriver

我正在尝试返回用逗号分隔的很长的url字符串。代码在控制台中工作正常,但是当我执行脚本时,urls_list,ruby变量为零。

require 'rubygems'
require 'selenium-webdriver'
require 'json'


driver = Selenium::WebDriver.for :firefox
    driver.manage.timeouts.implicit_wait = 20

driver.get "https://go.urbanairship.com/accounts/login/"
username = driver.find_element(:id, "id_username").send_keys "????"
password = driver.find_element(:id, "id_password").send_keys "????"
submit = driver.find_element(:class, "important").click

urls_list = driver.execute_script( "
     var urls = [];
     for (var i = 1; i < 37; i ++) {
       (function (i){
         $.getJSON('https://go.urbanairship.com/apps/-/list/'+ i + '/per_page=20').then(function(data) {   
            var json = data;
            for (var j = 0; j < json.objects.length; j++) {
              var url = json.objects[j].url
              urls.push(url);
             }  
          });
       }(i));

     }
     urls.toString();

")

sleep 10
puts urls_list # urls_list is nil

1 个答案:

答案 0 :(得分:1)

您必须向驱动程序提供一些关于脚本中返回内容的提示。

如同,将其更改为:

return urls.toString();