File is not being uploaded using RemoteWebDriver

43 Views Asked by At

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'

2

There are 2 best solutions below

0
sashkins On

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:

driver = new EdgeDriver();

to this:

driver = new RemoteWebDriver(new URL("..."), new ChromeOptions());

See the docs and this example

0
Dhru 'soni On

You can refer this Selenium Official document for more information.

https://www.selenium.dev/documentation/webdriver/drivers/remote_webdriver/

It will give you idea what is missing. Here, you need to find out the input box that have the tag with type=file. without type=file you can not upload anything. you required to use Robot class if you want to upload any thing.

You can refer to this git page and explore coding.

https://github.com/SeleniumHQ/seleniumhq.github.io/blob/trunk/examples/java/src/test/java/dev/selenium/drivers/RemoteWebDriverTest.java#L48-L51