在Splash中使用Lua脚本访问google.com的DOM

在Splash中使用Lua脚本访问google.com的DOM,lua,webautomation,splash-js-render,Lua,Webautomation,Splash Js Render,我试图在Splash中运行Lua脚本来执行Google搜索,并截取搜索结果的屏幕截图。当我尝试在我的Lua脚本中使用xpath或css选择器选择Google搜索框时,出现以下错误: { "error": 400, "type": "ScriptError", "description": "Error happened while executing Lua script"

我试图在Splash中运行Lua脚本来执行Google搜索,并截取搜索结果的屏幕截图。当我尝试在我的Lua脚本中使用xpath或css选择器选择Google搜索框时,出现以下错误:

{
    "error": 400,
    "type": "ScriptError",
    "description": "Error happened while executing Lua script",
    "info": {
        "message": "[string \"function main(splash, args)\r...\"]:9: cannot select the specified element {'type': 'JS_ERROR', 'js_error_type': 'SyntaxError', 'js_error_message': 'SyntaxError: DOM Exception 12', 'js_error': 'Error: SyntaxError: DOM Exception 12', 'message': \"JS error: 'Error: SyntaxError: DOM Exception 12'\"}",
        "type": "SPLASH_LUA_ERROR",
        "splash_method": "select",
        "source": "[string \"function main(splash, args)\r...\"]",
        "line_number": 9,
        "error": "cannot select the specified element {'type': 'JS_ERROR', 'js_error_type': 'SyntaxError', 'js_error_message': 'SyntaxError: DOM Exception 12', 'js_error': 'Error: SyntaxError: DOM Exception 12', 'message': \"JS error: 'Error: SyntaxError: DOM Exception 12'\"}"
    }
}
这是我的Lua脚本:

function main(splash, args)

  splash.private_mode_enabled = false
  splash:set_user_agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")
  
  assert(splash:go(args.url))
  assert(splash:wait(1.0))

  search_box = assert(splash:select("//div[@class='a4bIc']/input"))
  search_box:focus()
  search_box:send_text('my user agent')
  search_box:send_keys('<Enter>')
  assert(splash:wait(2.0))
  
  return splash:png()
end
主功能(飞溅,参数)
splash.private_mode_enabled=false
splash:set_user_agent(“Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:81.0)Gecko/20100101 Firefox/81.0”)
断言(splash:go(args.url))
断言(飞溅:等待(1.0))
search\u box=assert(splash:select(“//div[@class='a4bIc']]/input”))
搜索框:焦点()
搜索框:发送文本(“我的用户代理”)
搜索框:发送密钥(“”)
断言(飞溅:等待(2.0))
返回splash:png()
结束
我试图设置自定义标题,在私有模式下运行脚本,但没有任何效果。 但是,使用duckduckgo.com时,相同的脚本运行时没有错误,输出正确。当目标URL是google.com时,问题就来了。 我认为google检测到浏览器被一个机器人(脚本)控制,所以它禁用了对DOM树的访问


你知道如何让它工作吗?

你的选择器有问题

"//div[@class='a4bIc']/input"

打开网页,点击F12,然后使用inspector找出该输入字段的目标div类。它们的类名也可能是动态生成的,以使其混淆。

选择器有问题

"//div[@class='a4bIc']/input"

打开网页,点击F12,然后使用inspector找出该输入字段的目标div类。也有可能他们的类名是动态生成的,以使其混淆。

可能页面尚未完全下载/呈现

function main(splash, args)
    splash.private_mode_enabled = false
    splash:set_user_agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")

    local ok, reason = assert( splash:go(args.url) )

    if ok then
        local wait, increment, maxwait = 0, 0.1, 10
        while wait < maxwait and not splash:select("//div[@class='a4bIc']/input") do
            splash:wait(increment)  --  wait until it exists, or times out
            wait = wait +increment
        end
        if wait >= maxwait then
            print('Timed out')
        else
            search_box = splash:select("//div[@class='a4bIc']/input")
            search_box:focus()
            search_box:send_text('my user agent')
            search_box:send_keys('<Enter>')
            splash:wait(2.0)
            return splash:png()
        end
    else
        print( reason )  --  see if it tells you why
    end
end
主功能(飞溅,参数)
splash.private_mode_enabled=false
splash:set_user_agent(“Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:81.0)Gecko/20100101 Firefox/81.0”)
本地ok,reason=assert(启动:go(args.url))
如果可以的话
本地等待,增量,maxwait=0,0.1,10
while wait=maxwait,则
打印('超时')
其他的
search\u box=splash:select(“//div[@class='a4bIc']/input”)
搜索框:焦点()
搜索框:发送文本(“我的用户代理”)
搜索框:发送密钥(“”)
飞溅:等待(2.0)
返回splash:png()
结束
其他的
打印(原因)——看看它是否告诉你原因
结束
结束

可能该页面尚未完全下载/呈现

function main(splash, args)
    splash.private_mode_enabled = false
    splash:set_user_agent("Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0")

    local ok, reason = assert( splash:go(args.url) )

    if ok then
        local wait, increment, maxwait = 0, 0.1, 10
        while wait < maxwait and not splash:select("//div[@class='a4bIc']/input") do
            splash:wait(increment)  --  wait until it exists, or times out
            wait = wait +increment
        end
        if wait >= maxwait then
            print('Timed out')
        else
            search_box = splash:select("//div[@class='a4bIc']/input")
            search_box:focus()
            search_box:send_text('my user agent')
            search_box:send_keys('<Enter>')
            splash:wait(2.0)
            return splash:png()
        end
    else
        print( reason )  --  see if it tells you why
    end
end
主功能(飞溅,参数)
splash.private_mode_enabled=false
splash:set_user_agent(“Mozilla/5.0(X11;Ubuntu;Linux x86_64;rv:81.0)Gecko/20100101 Firefox/81.0”)
本地ok,reason=assert(启动:go(args.url))
如果可以的话
本地等待,增量,maxwait=0,0.1,10
while wait=maxwait,则
打印('超时')
其他的
search\u box=splash:select(“//div[@class='a4bIc']/input”)
搜索框:焦点()
搜索框:发送文本(“我的用户代理”)
搜索框:发送密钥(“”)
飞溅:等待(2.0)
返回splash:png()
结束
其他的
打印(原因)——看看它是否告诉你原因
结束
结束

也许,您应该检查是否提取了
args.url
;这不是验证码。谷歌可能会分析用户代理或以其他方式识别机器人。是的,@AlexanderMashin正在获取
args.url
。当我对代码中的
9-12
行进行注释时,剩下的代码按预期工作-它只返回google主页的屏幕截图。这意味着访问DOM树时出现了问题。也许,您应该检查是否提取了
args.url
;这不是验证码。谷歌可能会分析用户代理或以其他方式识别机器人。是的,@AlexanderMashin正在获取
args.url
。当我对代码中的
9-12
行进行注释时,剩下的代码按预期工作-它只返回google主页的屏幕截图。这意味着访问DOM树时出现了问题。我使用inspector工具进行了检查和交叉验证,但不幸的是选择器是正确的。我甚至从inspector工具复制了完整的xpath/css选择器,并在脚本中使用了它,但得到了相同的错误。我使用inspector工具进行了检查和交叉验证,但不幸的是,选择器是正确的。我甚至从inspector工具复制了完整的xpath/css选择器,并在脚本中使用了它,但出现了相同的错误。我执行了上述脚本,但不幸的是再次出现了相同的错误-第9行:无法选择指定的元素。这意味着页面正在下载/呈现。请尝试
splash:select('div.a4bIc input.gLFyf.gsfi')
这就是他们的CSS选择器在我的浏览器上显示的方式Wesome!现在可以了。谢谢但是我仍然不明白为什么它不能与xpath选择器一起工作。尝试在浏览器检查器工具中复制粘贴此xpath
//div[@class='a4bIc']/input
。如果选择了相同的元素,请告诉我。如果是,那么这在lua脚本中不起作用的原因是什么?不确定,这只是一种预感,因为手写笔中的脚本也是如此
add0n.com/stylus.html
…这是我首先说的-我执行了上面的脚本,但不幸的是又出现了相同的错误-第9行:无法选择指定的元素。这意味着页面正在下载/呈现。try
splash:select('div.a4bIc input.gLFyf.gsfi')
这就是他们的CSS选择器的工作方式