小编Rud*_*udy的帖子

错误 selenium.common.exceptions.JavascriptException:消息:ReferenceError:房间未定义

我试图使用 python 和 selenium 自动化基于 Web 的 API(haxball api)有两个步骤

  1. 使用浏览器控制台按钮访问https://html5.haxball.com/headlessF12并执行后var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });。执行后会出现验证码,我们必须手动解决。

  2. 解决后,您必须执行另一个脚本room.getPlayerList(); ,它将返回一个数组。

当我手动(使用浏览器和控制台)执行这两个步骤时,它可以完美运行,但是当我使用下面的代码(以 15 秒间隔手动解决验证码)进行自动化时,它在 15 秒等待时间(第 7 行)后出现错误)。

from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get("https://html5.haxball.com/headless")
time.sleep(5)
driver.execute_script("var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });")
time.sleep(15)
driver.execute_script("room.getPlayerList();")
Run Code Online (Sandbox Code Playgroud)

第一个 Javascript 执行正常,但第二driver.execute_script("room.getPlayerList();")个出现错误:

“selenium.common.exceptions.JavascriptException:消息:ReferenceError:房间未定义”

但是当我通过浏览器控制台一一输入它们时,这两个Javascript命令都成功执行。

javascript python api selenium automation

5
推荐指数
1
解决办法
6097
查看次数

标签 统计

api ×1

automation ×1

javascript ×1

python ×1

selenium ×1