Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:SeleniumHQ Selenium NetworkUtils

From Leeroopedia
Knowledge Sources
Domains WebDriver, Infrastructure
Last Updated 2026-02-12 00:00 GMT

Overview

NetworkUtils provides methods to resolve hostnames, IP addresses, and local network interface information for the current machine.

Description

NetworkUtils is a utility class in the org.openqa.selenium.net package that abstracts network interface querying to determine the hostname, host address, non-loopback addresses, and loopback IPv4 addresses of the local machine. It uses a pluggable NetworkInterfaceProvider (defaulting to DefaultNetworkInterfaceProvider) and includes platform-specific logic for macOS, Linux, and Windows. Results are cached to avoid repeated expensive DNS lookups. It also provides network diagnostics output for troubleshooting connectivity issues.

Usage

Use NetworkUtils when you need to programmatically determine the externally addressable hostname or IP address of the machine running Selenium, such as when configuring Selenium Grid nodes, mobile emulators that cannot access localhost, or when resolving loopback addresses for local test communication.

Code Reference

Source Location

Signature

public class NetworkUtils {

    public NetworkUtils()

    NetworkUtils(NetworkInterfaceProvider networkInterfaceProvider)

    public String getHostname()

    public String getHostAddress()

    public String getPrivateLocalAddress()

    public @Nullable String getNonLoopbackAddressOfThisMachine()

    public InetAddress getIp4NonLoopbackAddressOfThisMachine()

    public @Nullable String obtainLoopbackIp4Address()

    public @Nullable String getIpOfLoopBackIp4()

    public static String getNetWorkDiags()
}

Import

import org.openqa.selenium.net.NetworkUtils;

I/O Contract

Method Input Output Description
getHostname() none String -- hostname of the local machine Resolves hostname from environment variables (HOSTNAME, COMPUTERNAME), or by running the hostname command on macOS, or via InetAddress.getLocalHost() as a fallback. Cached after first call.
getHostAddress() none String -- IP address of the local machine Resolves the IP address, preferring reachable addresses on macOS en* interfaces. Falls back to InetAddress.getLocalHost().getHostAddress() or "127.0.0.1". Cached after first call.
getPrivateLocalAddress() none String -- IPv4 loopback address Returns the first local loopback IPv4 address found, or "127.0.0.1" if none found.
getNonLoopbackAddressOfThisMachine() none @Nullable String -- non-loopback IPv4 address Returns the host address string of the first non-loopback IPv4 network interface. Used by mobile emulators that cannot access localhost.
getIp4NonLoopbackAddressOfThisMachine() none InetAddress Returns the first non-loopback IPv4 InetAddress. Throws WebDriverException if none found.
obtainLoopbackIp4Address() none @Nullable String -- loopback IPv4 address or hostname Returns a loopback IPv4 address guaranteed to resolve to IPv4. Checks multiple strategies including Linux lo interface. Throws WebDriverException if unable to resolve.
getIpOfLoopBackIp4() none @Nullable String -- loopback IPv4 IP string Iterates all network interfaces to find a loopback-only IPv4 address.
getNetWorkDiags() none String -- diagnostic text Static method that dumps all network interface information for troubleshooting.

Usage Examples

// Get the hostname of the local machine
NetworkUtils networkUtils = new NetworkUtils();
String hostname = networkUtils.getHostname();

// Get the IP address
String address = networkUtils.getHostAddress();

// Get a non-loopback address for mobile emulator configuration
String nonLoopback = networkUtils.getNonLoopbackAddressOfThisMachine();

// Get loopback IPv4 address
String loopback = networkUtils.obtainLoopbackIp4Address();

// Dump network diagnostics for debugging
String diags = NetworkUtils.getNetWorkDiags();

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment