最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error selenium.common.exceptions.JavascriptException: Message: ReferenceError: room is not defined - Stack Overflow

programmeradmin1浏览0评论

I was trying to automate a web based API (haxball api) using python and selenium there were two steps

  1. After visiting using your browser console F12 button and execute this var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });. After executing a captcha will appear we have to solve it manually.

  2. After solving you have to execute another script room.getPlayerList(); it will return an array back.

When I manually (using browser and console) do both steps, it works perfectly, but when I automate as using the code below (solving captcha manually at the 15 second interval) it is giving an error after the 15 second wait time (7th line).

from selenium import webdriver
import time
driver=webdriver.Firefox()
driver.get("")
time.sleep(5)
driver.execute_script("var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });")
time.sleep(15)
driver.execute_script("room.getPlayerList();")

The first Javascript executes fine but the second driver.execute_script("room.getPlayerList();") gives an error:

"seleniummon.exceptions.JavascriptException: Message: ReferenceError: room is not defined"

but both the Javascript mands execute successfully when I enter them through the browser console one by one.

I was trying to automate a web based API (haxball api) using python and selenium there were two steps

  1. After visiting https://html5.haxball./headless using your browser console F12 button and execute this var room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });. After executing a captcha will appear we have to solve it manually.

  2. After solving you have to execute another script room.getPlayerList(); it will return an array back.

When I manually (using browser and console) do both steps, it works perfectly, but when I automate as using the code below (solving captcha manually at the 15 second interval) it is giving an error after the 15 second wait time (7th line).

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

The first Javascript executes fine but the second driver.execute_script("room.getPlayerList();") gives an error:

"selenium.mon.exceptions.JavascriptException: Message: ReferenceError: room is not defined"

but both the Javascript mands execute successfully when I enter them through the browser console one by one.

Share Improve this question edited Jun 14, 2018 at 19:29 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 14, 2018 at 16:08 RudyRudy 531 gold badge1 silver badge5 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

you can use it only together

from selenium import webdriver
driver=webdriver.Firefox()
driver.get('url')
driver.execute_script("""
    var foo = 'this is a test';
    console.log(foo);
""")

Update

but if we want to get our variable in another execute_script method we can defined our variables in window for example:

from selenium import webdriver
driver=webdriver.Firefox()
driver.get('url')
driver.execute_script("""
    window.foo = 'Window variable';
""")

# some code

driver.execute_script("""
    console.log(window.foo);
""")

Output

# In console
Window variable

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论