Validate urls using Python and Selenium Validate urls using Python and Selenium selenium selenium

Validate urls using Python and Selenium


You can use requests to get the HTTP status code

    import requests    import time     from selenium import webdriver    from selenium.webdriver.common.keys import Keys    user_url = input('Please enter a valid url:')    # send a get request to the page, and if the status code is not OK    # ask for a different url    def valid_url(url):        try:            req = requests.get(url)            while req.status_code != requests.codes['ok']:                  return valid_url(input('Please enter a valid url:'))        except Exception as ex:            print(f'Something went wrong: {ex}')            print('Try again!')            return valid_url(input('Please enter a valid url:'))        return url    url = valid_url(user_url)    driver = webdriver.Chrome()    driver.get(url) # funtion is called here    HEADERS = {'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36', 'accept': '*/*'}    time.sleep(8)    imagecounter = driver.find_elements_by_css_selector('img')    print('Number of HTML image tags:')    print(len(imagecounter))


To validate a user provided url before proceeding you can use Python's module to check the request status andyou can use the following solution:

  • Code Block:

    from selenium import webdriverimport requestswhile True:    user_url = str(input("Please enter a valid url:"))    req = requests.get(user_url)    if req.status_code != requests.codes['ok']:        print("Not a valid url, please try again...")        continue    else:        breakprint("URL was a valid one... Continuing...")driver = webdriver.Chrome(executable_path=r'C:\WebDrivers\chromedriver.exe')driver.get(user_url)# perform your rest of the tasks
  • Console Output:

    Please enter a valid url:https://www.goodday.comNot a valid url, please try again...Please enter a valid url:https://www.goodday.comNot a valid url, please try again...Please enter a valid url:https://www.goodday.comNot a valid url, please try again...Please enter a valid url:https://www.google.comURL was a valid one... Continuing...DevTools listening on ws://127.0.0.1:54638/devtools/browser/975e0993-166a-4144-a05f-dcfb1d9b29a2

Reference

You can find a couple of relevant discussions in: