I am initializing ChromeDriver on selenium grid using RemoteWebdriver, then I need to cast RemoteWebDriver to ChromeDriver?
If it is possible, can I have an example?
I am initializing ChromeDriver on selenium grid using RemoteWebdriver, then I need to cast RemoteWebDriver to ChromeDriver?
If it is possible, can I have an example?
ChromeDriver
is the extension of RemoteWebDriver
. If the actual object that is referred by RemoteWebDriver
is a ChromeDriver
you can use explicit casting.
Otherwise you will fail to cast since under RemoteWebDriver
some other extension might take the place (for example FirefoxDriver
also can be refered as RemoteWebDriver
) which might not adhere the contract ChromeDriver
implies (the same is applicable to a pure RemoteWebDriver
- the object simply does not contain the part that is specific for ChromeDriver
).
Till Selenium v3, ChromeDriver extends RemoteDriver. This ChromeDriver was a WebDriver implementation that controled the google-chrome browser running on the local machine.
However, selenium4 unleashes a new design/feature where,
As an example to intercept the Network you can do the following:
System.setProperty("webdriver.chrome.driver","C:\\WebDrivers\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
ChromeDriver driver = new ChromeDriver(options);
DevTools devTool = driver.getDevTools();
devTool.createSession();
devTool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
devTool.send(Network.setBlockedURLs(ImmutableList.of("*.jpg", "*.jpeg", "*.png", "*.css")));
driver.get("https://amazon.in/b?node=1375424031");
We have not need to cast Drivers, we have only need to initialise the WebDriver variable it can handle all the different type of driver.
WebDriver driver = null;
if (grid)
driver = new RemoteWebDriver(new URL("localhost:4444"));
else
driver = new ChromeDriver();