Implementation:SeleniumHQ Selenium Urls
| Knowledge Sources | |
|---|---|
| Domains | WebDriver, Infrastructure |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Urls is a utility class providing static methods for URL encoding/decoding with UTF-8 and for converting raw strings and URIs into properly formed URI and URL objects.
Description
Urls is a utility class in the org.openqa.selenium.net package that wraps Java's URLEncoder and URLDecoder with UTF-8 as the default charset, and provides intelligent conversion of raw strings (which may be plain hostnames, IPv4 addresses, IPv6 addresses, or full URIs) into java.net.URI objects. The from(String) method handles several edge cases: plain hostnames without a scheme get http:// prepended, numeric-prefix strings followed by a colon are treated as IP addresses rather than schemes, and strings with a recognized scheme (colon followed by slash) are parsed directly. The class also converts URI to URL via fromUri, which is preferred because URI does not require a registered scheme handler.
Usage
Use Urls when constructing WebDriver remote server URLs from user-provided strings that may or may not include a scheme, when encoding/decoding URL parameters in Selenium commands, or when converting between URI and URL representations throughout the Selenium codebase.
Code Reference
Source Location
- Repository: SeleniumHQ_Selenium
- File: java/src/org/openqa/selenium/net/Urls.java
Signature
public class Urls {
public static String urlEncode(String value)
public static String urlDecode(String encodedValue)
public static URL fromUri(URI uri)
public static URI from(String rawUri)
}
Import
import org.openqa.selenium.net.Urls;
I/O Contract
| Method | Input | Output | Description |
|---|---|---|---|
urlEncode(String value) |
value: text to encode |
String -- URL-encoded string |
Encodes the input using URLEncoder.encode with UTF-8 charset.
|
urlDecode(String encodedValue) |
encodedValue: URL-encoded text |
String -- decoded string |
Decodes the input using URLDecoder.decode with UTF-8 charset.
|
fromUri(URI uri) |
uri: a java.net.URI |
URL -- converted URL object |
Converts a URI to a URL. Throws UncheckedIOException if the URI is malformed for URL conversion.
|
from(String rawUri) |
rawUri: a raw string (hostname, IP, or full URI) |
URI -- parsed URI object |
Intelligently parses the input: plain hostnames get http:// scheme; IP addresses (IPv4 with port, abbreviated IPv6) get http:// scheme; strings with a recognized scheme are parsed directly. Throws UncheckedIOException wrapping URISyntaxException on invalid input. URI fragments are not handled.
|
Usage Examples
import org.openqa.selenium.net.Urls;
import java.net.URI;
import java.net.URL;
// URL encoding and decoding
String encoded = Urls.urlEncode("hello world & more");
// Result: "hello+world+%26+more"
String decoded = Urls.urlDecode("hello+world+%26+more");
// Result: "hello world & more"
// Convert a raw hostname to a URI
URI uri1 = Urls.from("localhost");
// Result: http://localhost
// Convert an IP:port to a URI
URI uri2 = Urls.from("192.168.1.1:4444");
// Result: http://192.168.1.1:4444
// Parse a full URI string
URI uri3 = Urls.from("https://grid.example.com:4444/wd/hub");
// Result: https://grid.example.com:4444/wd/hub
// Convert URI to URL
URL url = Urls.fromUri(uri3);
Related Pages
- SeleniumHQ_Selenium_UrlChecker -- URL availability polling utility in the same package
- SeleniumHQ_Selenium_NetworkUtils -- Network address resolution utility in the same package
- SeleniumHQ_Selenium_PortProber -- Free port detection utility in the same package