Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions GetLatestDownloadedFilePath/GetLatestDownloadPathChrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.example.enhancementseleniumscripts.GetLatestDownloadedFilePath;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class GetLatestDownloadPathChrome {

public static void main(String[] args) throws InterruptedException {

// Initialize the ChromeDriver
WebDriver driver = new ChromeDriver();

try {

driver.navigate().to("https://sample-files.com/documents/txt/");

WebElement element = driver.findElement(By.xpath("//*[@id=\"post-178\"]/div/div/p[3]/a"));
element.click();

Thread.sleep(3000);
// Navigate to the Chrome downloads page
driver.get("chrome://downloads/all");

try {
String filePath = (String) ((JavascriptExecutor) driver).executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList').items[0].filePath;");
if (filePath != null && !filePath.isEmpty()) {
System.out.println("Latest file path: " + filePath);
} else {
System.out.println("File path not found.");
}
} catch (Exception e) {
System.out.println("Error using JavaScript method: " + e.getMessage());
}

} finally {
// Close the browser
driver.quit();
}
}
}






70 changes: 70 additions & 0 deletions GetLatestDownloadedFilePath/GetLatestDownloadPathEdge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.example.enhancementseleniumscripts.GetLatestDownloadedFilePath;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.time.Duration;

public class GetLatestDownloadPathEdge {

public static void main(String[] args) {

WebDriver driver = new EdgeDriver();

try {
driver.navigate().to("https://sample-files.com/documents/txt/");
WebElement element = driver.findElement(By.xpath("//*[@id=\"post-178\"]/div/div/p[3]/a"));
element.click();
driver.get("edge://downloads/all");

Thread.sleep(Duration.ofSeconds(10).toMillis());

try {
String filePath = (String) ((JavascriptExecutor) driver).executeScript(
"function getLatestDownloadedFilePath() {" +
" const shadowRootImgElement = document.querySelector(\"body > downloads-app\")?.shadowRoot.querySelector(\"#downloads-item-1 > div.downloads_itemIconContainer.iDoExist > img\");" +
"" +
" if (shadowRootImgElement) {" +
" const filePath = new URL(shadowRootImgElement.src).searchParams.get('path');" +
" if (filePath) {" +
" return decodeURIComponent(filePath);" +
" }" +
" } " +
" const latestFileItem = document.querySelector('.downloads-list [role=\"listitem\"]');" +
"" +
" if (latestFileItem) {" +
" const fileIconImg = latestFileItem.querySelector('img');" +
" if (fileIconImg) {" +
" const filePath = new URL(fileIconImg.src).searchParams.get('path');" +
" if (filePath) {" +
" return decodeURIComponent(filePath);" +
" }" +
" }" +
" } " +
" return null;" +
"}" +
"return getLatestDownloadedFilePath();");

System.out.println("File path: " + filePath);

if (filePath != null && !filePath.isEmpty()) {
System.out.println("Latest file path: " + filePath);
} else {
System.out.println("File path not found.");
}
} catch (Exception e) {
System.out.println("Error using JavaScript method: " + e.getMessage());
}

} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();

} finally {
driver.quit();
}
}
}
68 changes: 68 additions & 0 deletions GetLatestDownloadedFilePath/GetLatestDownloadPathFirefox.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.example.enhancementseleniumscripts.GetLatestDownloadedFilePath;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.time.Duration;

public class GetLatestDownloadPathFirefox {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

try {
driver.navigate().to("https://sample-files.com/documents/txt/");
WebElement element = driver.findElement(By.xpath("//*[@id=\"post-178\"]/div/div/p[3]/a"));
element.click();
driver.get("about:downloads");

Thread.sleep(Duration.ofSeconds(10).toMillis());

try {
String filePath = (String) ((JavascriptExecutor) driver).executeScript(
"function getLatestDownloadedFilePath() {" +
" const richListBox = document.querySelector('richlistbox#downloadsListBox');" +
" if (!richListBox) {" +
" return null;" +
" }" +
" const latestDownloadItem = richListBox.querySelector('richlistitem');" +
" if (!latestDownloadItem) {" +
" return null;" +
" }" +
" const downloadTypeIcon = latestDownloadItem.querySelector('image.downloadTypeIcon');" +
" if (!downloadTypeIcon) {" +
" return null;" +
" }" +
" const filePath = downloadTypeIcon.getAttribute('src');" +
" if (!filePath) {" +
" return null;" +
" }" +
" const filePathFromUrl = filePath.split('?')[0].replace('moz-icon://', '');" +
" return filePathFromUrl;" +
"}" +
"return getLatestDownloadedFilePath();");

System.out.println("File path: " + filePath);

if (filePath != null && !filePath.isEmpty()) {
System.out.println("Latest file path: " + filePath);
} else {
System.out.println("File path not found.");
}
} catch (Exception e) {
System.out.println("Error using JavaScript method: " + e.getMessage());
}

} catch (Exception e) {
System.err.println("An error occurred: " + e.getMessage());
e.printStackTrace();

} finally {
driver.quit();
}
}
}