Adding User-Agent for Safari

How to add user-agent for safari?

1 Like

Hey Dipen,

To set the User-Agent for Safari, you can utilize the SafariOptions class. Below is an example code snippet demonstrating how to add the User-Agent in Safari:

// Import necessary libraries
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.safari.SafariOptions;
import java.net.URL;
import java.util.HashMap;

// Create SafariOptions instance
SafariOptions safariOptions = new SafariOptions();

// Set User-Agent in SafariOptions
safariOptions.addCapability("browserName", "safari");
safariOptions.addCapability("version", "latest");
safariOptions.addCapability("platform", "macOS 10.15");
safariOptions.addCapability("userAgent", "your-custom-user-agent-string");

// Other capabilities if needed
safariOptions.addCapability("LT:Options", ltOptions);

// Create RemoteWebDriver instance with SafariOptions
RemoteWebDriver driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + "@hub.lambdatest.com/wd/hub"), safariOptions);

In this code:

  • We create a SafariOptions instance.
  • We set the necessary capabilities for Safari, including the User-Agent (userAgent).
  • If there are additional capabilities needed (such as Lambdatest capabilities), they can be added as well.
  • Finally, we create a RemoteWebDriver instance using the configured SafariOptions.

Ensure that you replace “your-custom-user-agent-string” with the actual User-Agent string you want to use.

We appreciate your question! If there’s anything else you’re curious about, we’re here to assist. Thankyou so much.