Home > Software design >  Selenium-webdriver script breaks loop
Selenium-webdriver script breaks loop

Time:12-02

I had been tasked with fixing a digital sign loop running off python in the office. The original script was lost due to a OS crash and I had to recreate it. I am at my python limits on fixing what I had been able to create using selenium.

I wrote the below script and it functions for random periods of time before the loop breaks and the script must be executed again.

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

website = ["https://www.fireeye.com/cyber-map/threat-map.html",
"https://horizon.netscout.com/?sidebar=close",
"https://www.accuweather.com/en/us/minneapolis/55415/hourly- 
weather-forecast/348794?=page",
"https://www.accuweather.com/en/us/minneapolis/55415/daily- 
weather-forecast/348794?=page"
]

driver = webdriver.Chrome(r'/usr/bin/chromedriver') 
driver.get(website[0])
driver.maximize_window()

driver.execute_script("window.open('about:blank', 'secondtab');")
driver.switch_to.window("secondtab")
driver.get(website[1])

driver.execute_script("window.open('about:blank', 'thirdtab');")
driver.switch_to.window("thirdtab")
driver.get(website[2])
driver.execute_script("window.scrollBy(0,250);")

driver.execute_script("window.open('about:blank', 'fourthtab');")
driver.switch_to.window("fourthtab")
driver.get(website[3])
driver.execute_script("window.scrollBy(0,100);")

Can anyone tell me why the loop breaks?

The loop is a while true condition :

while True:

    if "FireEye" in driver.title:
        time.sleep(20)
        driver.switch_to.window(driver.window_handles[1])
        
    elif "Attack" in driver.title:
        time.sleep(20)
        driver.switch_to.window(driver.window_handles[2])
    
    elif "Hourly" in driver.title:
        time.sleep(10)
        driver.switch_to.window(driver.window_handles[3])
        
    elif "Daily" in driver.title:
        time.sleep(10)
        driver.switch_to.window(driver.window_handles[0])

The conditions are checking for the web tab titles of each site and as each should always be true.

It at a random intervals returns the following traceback error:

*driver.switch_to.window(driver.window_handles[3])

IndexError: list index out of range*

I cannot determine what is causing the index to no longer function.

CodePudding user response:

At the bottom of this, python thinks there isn't a fourth window open. So first, let's put in some error handling, something like this:

while True:
    try:
        if "FireEye" in driver.title:
            time.sleep(20)
            driver.switch_to.window(driver.window_handles[1])
        
        elif "Attack" in driver.title:
            time.sleep(20)
            driver.switch_to.window(driver.window_handles[2])
    
        elif "Hourly" in driver.title:
            time.sleep(10)
            driver.switch_to.window(driver.window_handles[3])
        
        elif "Daily" in driver.title:
            time.sleep(10)
            driver.switch_to.window(driver.window_handles[0])
    except:
        foreach w in driver.window_handles:
            print("{} is open!".format(w))

This will at least continue the loop regardless of the error, and tell you what tabs it thinks are open.

Edit: Also, as a note, window_handles is not ordered. So what may be window_handles[1] in one iteration may end up as window_handles[3] in the next.

  • Related