2017-01-12 12 views
1

ウェブサイトでscrapy + splashで使用したいluaスクリプトを作成しています。テキストを入力してボタンをクリックするスクリプトを作成したいと思います。私は自分のコードが正しく実行されるかどうかをテストするためにスプラッシュAPIを使用しています今スプラッシュAPI/luaエラー:ローカル要素のインデックスを作成しようとしています(ゼロ値)

function main(splash) 
    local url = splash.args.url 
    assert(splash:go(url)) 
    assert(splash:wait(5)) 

    local element = splash:select('.input_29SQWm') 
    assert(element:send_text("Wall Street, New York")) 
    assert(splash:send_keys("<Return>")) 
    assert(splash:wait(5)) 

    return { 
    html = splash:html(), 
    } 
end 

:私は、次のコードを持っています。 「レンダリング」をクリックすると次のメッセージが表示されます。

{ 
    "info": { 
     "message": "Lua error: [string \"function main(splash)\r...\"]:7: attempt to index local 'element' (a nil value)", 
     "type": "LUA_ERROR", 
     "error": "attempt to index local 'element' (a nil value)", 
     "source": "[string \"function main(splash)\r...\"]", 
     "line_number": 7 
    }, 
    "error": 400, 
    "type": "ScriptError", 
    "description": "Error happened while executing Lua script" 
} 

「Wall Street、New York」を送信しようとすると、何らかの理由で要素がまだゼロです。なぜか分からない。

$('.input_29SQWm') 

私は希望の要素を見つけました。

Q:誰かが間違っていることを知っていますか?

ありがとうございます!

+0

'nil'値は、' document.querySelector( '。input_29SQWm') 'が結果を返しなかったことを意味します。なぜこれがスプラッシュで起こったのですが、Chromeではどうなったのですか?それは伝えにくいです。スプラッシュに要素が表示されないようなエラーがあるかもしれません。デバッグには、最初の 'splash:wait()'の直後に 'splash:html()'と 'splash:png()'を返して、その要素がページに存在することを確認します。 –

答えて

3

エラーメッセージは、あなたがnilであるローカル '要素'のインデックスを作成しようとしていることを示しています。 エラーは7行目で発生します:assert(element:send_text("Wall Street, New York"))

なぜnilですか?

http://splash.readthedocs.io/en/stable/scripting-ref.html#splash-select

If the element cannot be found using the specified selector nil will be returned. If your selector is not a valid CSS selector an error will be raised.

あなたの間違いを処理していない:6行目では、我々は明らかにsplash:select('.input_29SQWm')戻りnilelement

local element = splash:select('.input_29SQWm') 

に値をasign

はのは、ドキュメントに見てみましょう選択がnilを返す場合があります。 nilの値を盲目的に索引付けすることはできません。 また、エラーを発生させる関数を呼び出すときは、保護された呼び出しを使用する必要があります。

なぜセレクタがそのセレクタを使用して要素を見つけなかったのかを知ることはあなた次第です。

私はあなたが引き続きLuaでエラー処理についてお読みになることをお勧めします。

関連する問題