Selenium webdriver issue with file paths

762 views Asked by At

I'm having an issue with Selenium standalone webdriver used with webdriver-manager npm module. I'm using the Firefox Gecko driver. I need to select a file from an HTML file input component. When I try this on my local machine or on BrowserStack I get the error:

"WebDriverError: File not found: /Users/christophergrigg/a.pdf"

const requestFile = By.id('requestFile');
driver.wait(until.elementLocated(requestFile));
const requestFileEl = driver.findElement(requestFile);
driver.wait(until.elementIsVisible(requestFileEl), TIMEOUT).click();
requestFileEl.sendKeys('/Users/christophergrigg/a.pdf');
requestFileEl.sendKeys(webdriver.Key.ENTER);

On Browser stack I'm using this path:

requestFileEl.sendKeys('C:\\Desktop\\documents\\pdf-sample2.pdf'); // Windows 7 / 8 / 8.1
1

There are 1 answers

2
Florent B. On

You need to provide the full path of the file. And if the file is not present on the machine running the remote instance, you'll also have to set the file detector to automatically upload the file.

On mac OS X:

var remote = require('selenium-webdriver/remote');
driver.setFileDetector(new remote.FileDetector);
driver.sendKeys('/Users/christophergrigg/Desktop/a.pdf');

, or Windows:

var remote = require('selenium-webdriver/remote');
driver.setFileDetector(new remote.FileDetector);
driver.sendKeys('C:\\Users\\christophergrigg\\Desktop\\a.pdf');