Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Last active September 15, 2022 09:28
Show Gist options
  • Save SarahElson/70c2a8dc40be433ba62a27eac6dc4e9e to your computer and use it in GitHub Desktop.
Save SarahElson/70c2a8dc40be433ba62a27eac6dc4e9e to your computer and use it in GitHub Desktop.
How To Download Files Using JavaScript and Selenium
async function example() {
//To wait for browser to build and launch properly
let driver = await new Builder().forBrowser("chrome").build();
try {
//To go to the test website from the browser with our code.
await driver.get("http://demo.automationtesting.in/FileDownload.html");
//To enter data inside the text area
await driver
.findElement(By.id("textbox"))
.sendKeys("I love testing!", Key.RETURN);
//To click on "Generate File" button
await driver.findElement(By.id("createTxt")).click();
//To click on "Download" link
await driver.findElement(By.id("link-to-download")).click();
//Wait for 5s till download is completed
await driver.sleep(5000);
} catch (e) {
console.log("Error Occured:", e.name);
}
//It is always a safe practice to quit the browser after execution
await driver.quit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment