Skip to content

Commit d687fcc

Browse files
committed
Add a method to determine if a URL is valid
1 parent fa85664 commit d687fcc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

seleniumbase/fixtures/page_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
This module contains useful utility methods.
33
"""
4+
import re
45
import requests
56

67

@@ -42,6 +43,21 @@ def is_xpath_selector(selector):
4243
return False
4344

4445

46+
def is_valid_url(url):
47+
regex = re.compile(
48+
r'^(?:http)s?://' # http:// or https://
49+
r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+'
50+
r'(?:[A-Z]{2,6}\.?|[A-Z0-9-]{2,}\.?)|' # domain...
51+
r'localhost|' # localhost...
52+
r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})' # ...or ip
53+
r'(?::\d+)?' # optional port
54+
r'(?:/?|[/?]\S+)$', re.IGNORECASE)
55+
if regex.match(url):
56+
return True
57+
else:
58+
return False
59+
60+
4561
def _download_file_to(file_url, destination_folder, new_file_name=None):
4662
if new_file_name:
4763
file_name = new_file_name

0 commit comments

Comments
 (0)