I've wrote a simple code in Python that browse a few web pages stored in tuple. The goal is to regularly browse those pages and check if they are online and responding. Sample code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Chrome()
links = ('https://stackoverflow.com/', 'https://stackexchange.com/')
while True:
for url in links:
try:
browser.get(url)
print('OK')
except Exception as e:
print('Not OK: {}'.format(e))
But this code prints 'OK' even when I am offline, so I guess the get() method doesn't handle any exception if the actual page is not reached.
I've thought about using WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, 'some_common_id')))
to check if I have reached the actual page but my tuple may contain different url addresses with no common ID, class or element...
Then I thought about reversing this and using WebDriverWait(browser, 5).until(EC.presence_of_element_located((By.ID, 'main-frame-error')))
to search for a specific element you get in Google Chrome when offline (on their "Dino game" page).
But in this case the script would have to wait for 5 seconds every time the page from my tuple is reached successfully which would unnecessarily slow down the whole test.
I think there has to be a simpler way to browse a page with selenium and know if it is actually reached but I haven't been able to find a solution yet.