3

How can I get hold of the following object in python selenium:

(in firefox/Chrome console):
>>window.Alarms.alarmData
Array [ {…}, {…} ]

This returns empty:

alarm = driver.execute_script("return window.Alarms.alarmData;")
print(alarm)
[]

2
  • What is Alarms in this case? Commented Jan 14, 2019 at 15:44
  • Alarms: {alarmData: Array(2), ruledefaults: {…}, rules: {…}, location: "", suppress_sound: 0, …} Commented Jan 14, 2019 at 15:55

2 Answers 2

3

You can try to wait until JS object returns any non-empty value:

from selenium.webdriver.support.ui import WebDriverWait

alarm = WebDriverWait(driver, 20).until(lambda driver: driver.execute_script("return window.Alarms.alarmData;"))
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, Waiting helps, If I do return windows.Alarms; I get a large dict: {'alarmData': [], 'augmentSingleAlarm': {}, 'bIgnoreError': 0, 'bLog': 0, 'bPrefServer': 0, 'bSkipHtml': 0, 'bSkipOutputUser': 0, .. but alarmData is Empty. Even though I see it in console.
@MortenB So return window.Alarms.alarmData; still returns you unexpected output or lead to TimeOutException?
it returns an empty list, I found that if I add a mandatory time.sleep(10) it works, I should add a driverWait until list is no longer empty.
@MortenB , I guess Python interpreter doesn't "recognize" JS empty list as Python empty list (check whether print(type(alarm)) returns list or str)... Yes try until(lambda driver: driver.execute_script("return window.Alarms.alarmData;") != "[]")
1

With the help of @Andersson I found this issue. When starting window.Alarms.AlarmData is defined as an empty list '[]', so it will be successfully returned, I just need to wait until it is no longer an empty list, The important lession was to check initial state of variables, because they may be defined and just successfully in selenium.

def test2_alarmdata(self):
    """ fetch the data objects """
    driver = self.driver
    cookie_read(driver)
    driver.get(default['url'] + "/alarm_board/index.html")
    alarmData = None
    retries = args.retries
    while not alarmData and retries:
        time.sleep(1)
        alarms = driver.execute_script("return window.Alarms.alarmData;")
        if alarms:
            alarmData = alarms
        retries -= 1
    if isinstance(alarmData, list):
        print("we have {} queued alarms".format(len(alarmData)))

Run the unittest:

we have 2 queued alarms
__main__.T.test2_alarmdata (3.4068s)

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.