2

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.

Alex Makarenko
  • 687
  • 1
  • 6
  • 15
Miro
  • 47
  • 8

2 Answers2

1

If your usecase is to know if the page is offline or not responding you don't even need Selenium and you can simply use the requests.head() method from python-requests as follows:

  • Code Block:

    import requests
    links = ['https://stackoverflow.com/', 'https://stackexchange.com/'] 
    for link in links:
        print(requests.head(link))
    
  • Console Output:

    <Response [200]>
    <Response [200]>
    

Note: As per the current implementation, Selenium when invoking get() method actually uses python-requests module too.

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • 1
    Thank you, this works for my usecase. As the requests.head() returns Response object the code itself can be accessed by requests.head(link).status_code. – Miro Aug 27 '18 at 11:52
-1

Selenium doesn't provide module to get http status code. Therefore, you need to use another module such as request module. Here is the similar question.

How to get HTTP Response Code using Selenium WebDriver

Bumhwan Kim
  • 282
  • 3
  • 15