I am using below code to change the geolocation but it is not working? Can anyone provide me some better solution?

19 Views Asked by At

// Set up Chrome options with geolocation information ChromeOptions chromeOptions1 = new ChromeOptions(); chromeOptions1.addArguments("--disable-geolocation"); // Disable geolocation prompt chromeOptions1.addArguments("--use-fake-ui-for-media-stream"); // Allow media stream access chromeOptions1.addArguments("--enable-geolocation"); chromeOptions1.addArguments("--geolocation=latitude,longitude"); // For Headless below Three /* * chromeOptions1.addArguments("--no-sandbox"); * chromeOptions1.addArguments("--disable-dev-shm-usage"); * chromeOptions1.addArguments("--headless", "--window-size=1368,768"); */ // Create a new instance of the ChromeDriver with options driver = new ChromeDriver(chromeOptions1); driver.manage().deleteAllCookies(); driver.manage().timeouts().pageLoadTimeout(com.traval.qa.utility.TestUtilGD.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS); driver.get(ConfigureProperties.URL);

    // Execute Chrome DevTools Protocol (CDP) command to get geolocation information
    Object result = ((ChromeDriver) driver).executeCdpCommand("Emulation.setGeolocationOverride",
            Map.of("latitude", 30.2500, "longitude", -97.7500, "accuracy", 1));
    // Check if the geolocation is set successfully
    if (result != null) {
        System.out.println("Texas Geolocation is set successfully.");

        // Perform any other actions on the website with the modified geolocation

    } else {
        System.out.println("Failed to set geolocation.");
    }

Need to set Multiple location like texas and Germany

0

There are 0 best solutions below