【发布时间】:2018-11-24 11:38:01
【问题描述】:
我正在尝试使用 python 和 selenium 自动化基于 Web 的 API (haxball api) 有两个步骤
使用浏览器控制台 F12 按钮访问https://html5.haxball.com/headless 后,执行此
var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });。 执行后会出现验证码,我们必须手动解决。解决后你必须执行另一个脚本
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();")
第一个 Javascript 执行良好,但第二个 driver.execute_script("room.getPlayerList();") 给出错误:
“selenium.common.exceptions.JavascriptException:消息:ReferenceError:房间未定义”
但是当我通过浏览器控制台一一输入时,这两个Javascript命令都执行成功。
【问题讨论】:
标签: javascript python api selenium automation