'Problem with devTools.send(Emulation.setDeviceMetricsOverride selenium 4

Selenium 4:

I have an error on this line of code :

    devTools.send(Emulation.setDeviceMetricsOverride(600, 1000, 50, true, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));

the error:

The method setDeviceMetricsOverride(Integer, Integer, Number, Boolean, Optional, Optional, Optional, Optional, Optional, Optional, Optional, Optional, Optional) in the type Emulation is not applicable for the arguments (int, int, int, boolean, Optional, Optional, Optional, Optional, Optional, Optional, Optional, Optional, Optional)



Solution 1:[1]

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v85.emulation.Emulation;


public class CDPFeatures {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        System.setProperty("webdriver.chrome.driver","D:\\chromedriver_win32\\chromedriver.exe");
        
        ChromeDriver driver = new ChromeDriver();
        DevTools devTools = driver.getDevTools();
        devTools.createSession();
        
        Map deviceMetrics = new HashMap()
        {{
            put("width", 600);
            put("height", 1000);
            put("mobile", true);
            put("deviceScaleFactor", 50);
        }};
        driver.executeCdpCommand("Emulation.setDeviceMetricsOverride", deviceMetrics);
        
        driver.get("https://google.com");
        
        

    }

}

Solution 2:[2]

Even I faced this issue but through some hit and trial method I was able to solve it. Hopefully it might help you. posting my code below:

import java.net.MalformedURLException;
import java.util.Optional;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v101.emulation.Emulation;
import org.openqa.selenium.devtools.v101.emulation.model.DisplayFeature;
import org.openqa.selenium.devtools.v101.emulation.model.ScreenOrientation;
import org.openqa.selenium.devtools.v101.page.model.Viewport;

public class EmulationCDP {
public static void main(String[] args) throws MalformedURLException {
    // TODO Auto-generated method stub
     
    
  System.setProperty("webdriver.chrome.driver","G:\\Driver\\chromedriver.exe");
    ChromeDriver driver = new ChromeDriver();
    DevTools devTools = driver.getDevTools();
    devTools.createSession();
    devTools.send(Emulation.setDeviceMetricsOverride(700, 1000, 50, true, Optional.<Number> empty(),
            Optional.<Integer> empty(), Optional.<Integer> empty(), Optional.<Integer> empty(), 
            Optional.<Integer> empty(), Optional.<Boolean> empty(), Optional.<ScreenOrientation> empty(), 
            Optional.<Viewport> empty(), Optional.<DisplayFeature> empty()));
    driver.get("https://www.google.com/");
    }
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saksham
Solution 2