Arash Izmirov
Arash Izmirov

Reputation: 23

Validate urls using Python and Selenium

i want to do some basic url validation,and if url is invalid,request should not be proceed unless user have entered a valid one. Update: To be more clear I do not want the browser to be opened and image counter scipt to be runed unless the entered Url is valid.

import time 
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

user_url = input('Please enter a valid url:')
driver = webdriver.Chrome('/home/m/Desktop/chromedriver')
driver.get(user_url)
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))

Could you please modify the code and explain what is happening? I have tried with some libraries, but i think because of my poor coding skills there was no luck.

Upvotes: 2

Views: 3153

Answers (2)

Petru Tanas
Petru Tanas

Reputation: 1237

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))

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193128

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 webdriver
    import requests
    
    while 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:
            break
    print("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.com
    Not a valid url, please try again...
    Please enter a valid url:https://www.goodday.com
    Not a valid url, please try again...
    Please enter a valid url:https://www.goodday.com
    Not a valid url, please try again...
    Please enter a valid url:https://www.google.com
    URL 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:

Upvotes: 0

Related Questions