0

I was making an python program that executes a java script after loading a webpage it is a web based api where we can execute javascript commands through console of your browser (f12) ( api documentation)
This executing gives back an array, I just want to get this output of execution (the array) to be print. the code i used is given below. the time delay of 15 seconds is used for solving an captcha which have to do manually. executing getPlayerList(); command will give back you a list of players that is currently playing.

from selenium import webdriver
import time
import os
driver=webdriver.Firefox()
driver.get("https://html5.haxball.com/headless")
time.sleep(5)
driver.execute_script("""
window.room = window.HBInit({ roomName: 'botts', maxPlayers: 16 });
""")
#the time dalay is to solve the captcha manually that appear on the browser after executing above javascript.
time.sleep(15)
print(driver.execute_script(""" 
console.log(window.getPlayerList();
"""))

The above program works fine, but i have to print the output of getPlayerList(); I tried with return and console log, but that is not working for me.

3
  • where you define getPlayerList() ? Commented Jun 15, 2018 at 14:03
  • 1
    If I understood correctly, getPlayerList() is a function that's defined in the webpage you're scraping. You can't use functions that aren't defined within the scope of your code. Commented Jun 15, 2018 at 14:08
  • the web page i was running html5.haxball.com/headless it is a web based api where we can execute javascript commands through console of your browser (f12) ( api documentation github.com/haxball/haxball-issues/wiki/Headless-Host) . executing getPlayerList(); command will give back you a list of players that is currently playing. getPlayerList(); is already defined in their api. Commented Jun 15, 2018 at 14:29

2 Answers 2

1

you need to define your getPlayerList() function take this as example:

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

    // define the getPlayerList() functio;
    window.getPlayerList = function() {
        // here you make all the functional that you need
        return 'All players list';
    }

""")

driver.execute_script(""" 
    // now that we define our function, we can call it
    console.log(window.getPlayerList();
""")
Sign up to request clarification or add additional context in comments.

Comments

0

I am Agree with the @zimdero answer and the @GalAbra comment if you use Selenium.


But if you have absolutely the requirement to execute the javascript function which is present in the loaded page, you may achieve this by use of PyQt and QwebView class, with something like this:

browser = QwebView()
browser.load(QUrl("https://html5.haxball.com/headless"))
frame = browser.page().currentFrame()
#do your stuff here
#execute js function inside the webpage
frame.evaluateJavaScript(QString("getPlayerList()"))

For more information, take a look on How to call javascript function from PyQT

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.