I am trying to upload a file in a demo site using remoteWebDriver but it is not getting uploaded. I have successfully implemented with the WebDriver driver but was trying using RemoteWebDriver. The exception I am getting is :"org.openqa.selenium.WebDriverException: Setting the file detector only works on remote webdriver instances obtained via RemoteWebDriver". Can someone please help?
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class UsingRemoteWebDriver {
WebDriver driver;
String url = "https://practice-cybertekschool.herokuapp.com/upload";
By browseButton = By.id("file-upload");
By uploadButton = By.id("file-submit");
@BeforeTest
public void setUp() {
driver = new EdgeDriver();
driver.manage().window().maximize();
driver.get(url);
}
@Test
public void uploadFile() throws InterruptedException {
File uploadFile = new File("C:\\Users\\ACER\\Desktop\\test.au3");
((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
WebElement browseElement = driver.findElement(browseButton);
browseElement.sendKeys(uploadFile.getAbsolutePath());
Thread.sleep(5000);
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
`
Exception is: org.openqa.selenium.WebDriverException: Setting the file detector only works on remote webdriver instances obtained via RemoteWebDriver Build info: version: '4.10.0', revision: 'c14d967899'
The error you see is caused by the fact, that your driver is actually an EdgeDriver
To set the file detector you should use an instance of the RemoteWebDriver as the error suggests.
So you have to change this:
to this:
See the docs and this example