Deep
Deep

Reputation: 3

How to get what's inside a Javascript <script> from a webpage using Selenium on Python

I want to get some data from the "BATTLE_SERVER_DATA" in this piece of code off a webpage.

<hovercard-details></hovercard-details><script charset="utf-8" id="BATTLE_SERVER_DATA">
var BATTLE_TEXTS = $j.extend({}, {"consume_food":"Consume Food","cosume_energy_bar_text":"Consume Energy Bar","cosume_winter_treat_text":"Consume Ice Creams","buy_health_kit":"Get more energy","event_boss_error_1":"Nice try, but you cannot kill your enemies with the Silver Stake!","event_boss_error_2":"You must use the Silver Stake to defeat Dracula!","won_zone":"won the previous battle","division":"Division","the_airforce":"The Airforce","of":"of","won_division":"won this battle","won_battle":"won this campaign","yourHealth":"Your Energy:","opponentHealth":"Opponent Energy:","defenderHero":"Our battle hero","attackerHero":"Enemy Battle Hero","processing_battle":"Processing battle results","military_rank":"Military rank:","get_ready":"get ready","duel":"duel","fight":"Fight","ticket_info_msg":"No travelling costs will be deducted, instead a Q%%1%% ticket will be used.","notEnoughMoney":"You don't have enough citizenship currency (%%1%%).","fightAgainstBrothersTitle":"","fightAgainstBrothersBody":""}),
    SERVER_DATA = $j.extend(SERVER_DATA, {"initialLoad":true,"battleId":138240,"countryId":165,"mySideCountryId":165,"invaderId":165,"defenderId":1,"leftBattleId":1,"rightBattleId":165,"realInvaderId":165,"realDefenderId":1,"zoneId":12,"currentRoundNumber":12,"battleFinished":0,"isResistance":true,"isCivilWar":false,"mustInvert":false,"spectatorOnly":true,"onAirforceBattlefield":true,"battleZoneSituation":{"1":0,"2":0,"3":0,"4":0,"11":0},"points":{"defender":0,"attacker":50},"battle_critical_at":new Date('Wed, 08 Aug 2018 17:30:02 -0700'),"battle_start_at":new Date('Wed, 08 Aug 2018 15:30:02 -0700'),"server_time":new Date('Wed, 08 Aug 2018 15:35:31 -0700'),"zoneElapsedTime":"0:5:29","battle_close_to_finish":0,"citizenId":7662082,"militaryUnitId":162,"regimentId":316,"division":11,"currentDivision":11,"weaponSkinId":0,"opponentSkinId":0,"canSwitchDivisions":true,"health":595,"level":56,"currentWeaponId":-1,"hasBazookaAmmo":-1,"showTravelPopup":true,"fightAgainstBrothers":false,"travelRequired":true,"countries":{"167":"Albania","27":"Argentina","169":"Armenia","50":"Australia","33":"Austria","83":"Belarus","32":"Belgium","76":"Bolivia","69":"Bosnia-Herzegovina","9":"Brazil","42":"Bulgaria","23":"Canada","64":"Chile","14":"China","78":"Colombia","63":"Croatia","171":"Cuba","82":"Cyprus","34":"Czech-Republic","55":"Denmark","165":"Egypt","70":"Estonia","39":"Finland","11":"France","168":"Georgia","12":"Germany","44":"Greece","13":"Hungary","48":"India","49":"Indonesia","56":"Iran","54":"Ireland","58":"Israel","10":"Italy","45":"Japan","71":"Latvia","72":"Lithuania","66":"Malaysia","26":"Mexico","80":"Montenegro","31":"Netherlands","84":"New-Zealand","170":"Nigeria","73":"North-Korea","37":"Norway","57":"Pakistan","75":"Paraguay","77":"Peru","67":"Philippines","35":"Poland","53":"Portugal","81":"Republic-of-China-Taiwan","79":"Republic-of-Macedonia-FYROM","52":"Republic-of-Moldova","1":"Romania","41":"Russia","164":"Saudi-Arabia","65":"Serbia","68":"Singapore","36":"Slovakia","61":"Slovenia","51":"South-Africa","47":"South-Korea","15":"Spain","38":"Sweden","30":"Switzerland","59":"Thailand","43":"Turkey","40":"Ukraine","166":"United-Arab-Emirates","29":"United-Kingdom","74":"Uruguay","24":"USA","28":"Venezuela"},"regionsInvolved":[],"tickets":[],"rocket_damage":750000,"bomb_damage":5000000,"specialWeapons":[],"canUseCatchupBooster":true,"regimentBoosters":[],"regimentBooster":false,"eventDamageBoosters":[],"eventDamageBooster":false,"boosters":{"active":[],"inactive":{"speedBoosters":{"100_speedBoosters_3_600":{"name":"x9 Damage Accelerator","id":"100_speedBoosters_3_600","industryId":100,"quality":3,"amount":1,"activable":1,"deactivable":0,"activableFromInventory":1,"activableFromBattlefield":1,"activationData":{"tooltip":"x9 Damage Accelerator","url":"/en/military/fight-activateBooster","params":{"type":"speed","quality":3,"duration":600}},"active":0,"icon":0,"tooltip":"x9 Damage Accelerator for 10 minutes","token":"","attributes":{"damageAcceleration":{"id":"damageAcceleration","name":"Hit 9 times faster","type":"use","value":"x9"},"duration":{"id":"duration","name":"Duration","type":"minutes","value":600}},"isRaw":0,"isPartial":0,"type":"speedBoosters","duration":600,"isBooster":1,"isPackBooster":0,"canActivateBooster":1},"100_speedBoosters_2_600":{"name":"x5 Damage Accelerator","id":"100_speedBoosters_2_600","industryId":100,"quality":2,"amount":106,"activable":1,"deactivable":0,"activableFromInventory":1,"activableFromBattlefield":1,"activationData":{"tooltip":"x5 Damage Accelerator","url":"/en/military/fight-activateBooster","params":{"type":"speed","quality":2,"duration":600}},"active":0,"icon":0,"tooltip":"x5 Damage Accelerator for 10 minutes","token":"","attributes":{"damageAcceleration":{"id":"damageAcceleration","name":"Hit 5 times faster","type":"use","value":"x5"},"duration":{"id":"duration","name":"Duration","type":"minutes","value":600}},"isRaw":0,"isPartial":0,"type":"speedBoosters","duration":600,"isBooster":1,"isPackBooster":0,"canActivateBooster":1},"100_speedBoosters_1_300":{"name":"x2 Damage Accelerator","id":"100_speedBoosters_1_300","industryId":100,"quality":1,"amount":176,"activable":1,"deactivable":0,"activableFromInventory":1,"activableFromBattlefield":1,"activationData":{"tooltip":"x2 Damage Accelerator","url":"/en/military/fight-activateBooster","params":{"type":"speed","quality":1,"duration":300}},"active":0,"icon":0,"tooltip":"x2 Damage Accelerator for 5 minutes","token":"","attributes":{"damageAcceleration":{"id":"damageAcceleration","name":"Hit 2 times faster","type":"use","value":"x2"},"duration":{"id":"duration","name":"Duration","type":"minutes","value":300}},"isRaw":0,"isPartial":0,"type":"speedBoosters","duration":300,"isBooster":1,"isPackBooster":0,"canActivateBooster":1}},"prestigePointsBoosters":{"100_prestigePointsBoosters_1_301":{"name":"+1 Prestige Points Booster","id":"100_prestigePointsBoosters_1_301","industryId":100,"quality":1,"amount":2,"activable":1,"deactivable":0,"activableFromInventory":1,"activableFromBattlefield":1,"activationData":{"tooltip":"Your Prestige Points Booster availability","url":"/en/military/fight-activateBooster","params":{"type":"prestige_points","quality":1,"duration":301}},"active":0,"icon":0,"tooltip":"Increase your prestige by 1 for 5.0166666666667 minutes","token":"","attributes":{"prestigeBoost":{"id":"prestigeBoost","name":"+1 Prestige Points","type":"use","value":1},"duration":{"id":"duration","name":"Duration","type":"minutes","value":301}},"isRaw":0,"isPartial":0,"type":"prestigePointsBoosters","duration":301,"isBooster":1,"isPackBooster":0,"canActivateBooster":1}}},"canUseCatchupBooster":true,"events":[]},"has_jid":1,"pvpKillCount":0,"pvpShowLock":false,"tutorial":0,"mission1Completed":true,"mission2Completed":true,"mission3Completed":true,"hasActiveObjective":false,"hasDestroyedObjective":false,"canDeployObjective":false,"aoe_effects":[],"battle_effects":{"valentinesDay":{"is_active":0}},"aoeMyDivision":true,"aoeOtherDivisionData":[],"fightersData":[],"historyStats":[],"culture":"en","csrfToken":"9f030d06b52292457b7a1a1b1907d043","isLoggedIn":true,"ERPK_ECONOMY_HOST":"economy.erepublik.com","ERPK_MAIN_HOST":"www.erepublik.com","extras":"Ice Creams","texts":{"consume_food":"Consume Food","cosume_energy_bar_text":"Consume Energy Bar","cosume_winter_treat_text":"Consume Ice Creams","buy_health_kit":"Get more energy","event_boss_error_1":"Nice try, but you cannot kill your enemies with the Silver Stake!","event_boss_error_2":"You must use the Silver Stake to defeat Dracula!","won_zone":"won the previous battle","division":"Division","the_airforce":"The Airforce","of":"of","won_division":"won this battle","won_battle":"won this campaign","yourHealth":"Your Energy:","opponentHealth":"Opponent Energy:","defenderHero":"Our battle hero","attackerHero":"Enemy Battle Hero","processing_battle":"Processing battle results","military_rank":"Military rank:","get_ready":"get ready","duel":"duel","fight":"Fight","ticket_info_msg":"No travelling costs will be deducted, instead a Q%%1%% ticket will be used.","notEnoughMoney":"You don't have enough citizenship currency (%%1%%).","fightAgainstBrothersTitle":"","fightAgainstBrothersBody":""}}),
    regionsInvolved = [],
    currentWeaponId = -1,
    hasBazookaAmmo = -1,
    tickets = [],
    ticket_info_msg = BATTLE_TEXTS['ticket_info_msg'],
    notEnoughMoney = BATTLE_TEXTS['notEnoughMoney'],
    erepublik = erepublik || {},
    currentDivision = SERVER_DATA.onAirforceBattlefield ? 11 : SERVER_DATA.division,
    allDivisions = [1, 2, 3, 4, 11];

The methodology I've been trying is the following:

def checkbattle():
    browser.get(BattleLink)
    checkBattleData = browser.execute_script('return BATTLE_SERVER_DATA;')
    print(checkBattleData)

It doesn't work as I need it to and returns

<selenium.webdriver.remote.webelement.WebElement (session="fb78e0c364bfe0b31fb94aa701298789", element="0.7451343445847121-1")>

My main goal is to retrieve the data present in there, for example I might like to find whether or not "initialLoad" in SERVER_DATA inside BATTLE_SERVER_DATA is true or not. What's the best way to achieve this?

Upvotes: 0

Views: 70

Answers (1)

JeffC
JeffC

Reputation: 25597

To clarify things... browser.execute_script() is the way you can execute Javascript code through Selenium. So the return BATTLE_SERVER_DATA; code you are executing is basically returning the value of a variable, BATTLE_SERVER_DATA, which from the HTML you've included is null/empty. The only reference I see to BATTLE_SERVER_DATA in the HTML you provided is that it's the ID of a SCRIPT tag so from that I'm assuming you want to get the code/script contained inside of that SCRIPT tag. If that's what you are trying to do, you can use Selenium to get the tag and then pull the innerHTML of that element to get the text/script inside.

def checkbattle():
    browser.get(BattleLink)
    checkBattleData = browser.find_element_by_id('BATTLE_SERVER_DATA').get_attribute('innerHTML')
    print(checkBattleData)

From there you can parse out the pieces that you want.

Upvotes: 2

Related Questions