Skip to content

[🚀 Feature]: [py] Validate URL's before navigation #16619

@cgoldberg

Description

@cgoldberg

Description

When you call driver.get() or driver.browsing_context.navigate()), it attempts to navigate to a URL, even if the URL is malformed.

Browsers don't handle this very well. For example...

if you do:

driver.get("example.com")

or

driver.get("http//example.com")

Chrome will just not navigate and not return any error (Firefox returns an error).

Proposed change:

If we validate the URL before attempting navigation, we can raise a useful exception: raise InvalidArgumentException("Invalid URL").

Here is some example code for validation:

from urllib.parse import urlparse

def is_valid_url(url):
    try:
        result = urlparse(url)
        return bool(result.scheme)
    except AttributeError:
        return False

This validates it can be parsed as a URL and contains a scheme.

Have you considered any alternatives or workarounds?

No response

Does this apply to specific language bindings?

Python

What part(s) of Selenium does this relate to?

No response

Metadata

Metadata

Labels

C-pyPython BindingsI-enhancementSomething could be better

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions