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.
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();
""")
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.