diff --git a/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameChrome.java b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameChrome.java new file mode 100644 index 0000000..71f33e8 --- /dev/null +++ b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameChrome.java @@ -0,0 +1,61 @@ +package org.example.enhancementseleniumscripts.GetDownloadedFilePathUsingFileName; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; +import java.util.List; +import java.util.Map; + +public class GetDownloadPathUsingFileNameChrome { + + public static void main(String[] args) throws InterruptedException { + + // Initialize the ChromeDriver + WebDriver driver = new ChromeDriver(); + String targetFileName = "simple.txt"; // Replace with your desired file name + + try { + // Download the file twice to ensure it's present in downloads and to trigger the download twice + + String downloadURL = "https://drive.usercontent.google.com/download?id=1szYMoC4D-I5uZqiovJ5IOSwh8jVOVpmK&export=download&authuser=0&confirm=t&uuid=770e07f0-0783-4b5c-9c09-b36741378e9f&at=AEz70l58Um2in724t0ss8cJtPG7k:1740249548154"; + + driver.get(downloadURL); //Initiate the download + Thread.sleep(3000); //Wait for download to start + + driver.get(downloadURL); //Initiate the download again to see the downloaded history + + Thread.sleep(3000); //Wait for download to start + + // Navigate to the Chrome downloads page + driver.get("chrome://downloads/all"); + + try { + // Execute JavaScript to get the list of download items + List> downloadItems = (List>) ((JavascriptExecutor) driver).executeScript( + "return Array.from(document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList').items).map(item => ({fileName: item.fileName, filePath: item.filePath}));" + ); + + String filePath = null; + for (Map item : downloadItems) { + String fileName = (String) item.get("fileName"); + if (fileName != null && fileName.equals(targetFileName)) { + filePath = (String) item.get("filePath"); + break; // Found the file, no need to continue searching + } + } + + if (filePath != null && !filePath.isEmpty()) { + System.out.println("File path for " + targetFileName + ": " + filePath); + } else { + System.out.println("File " + targetFileName + " not found in downloads."); + } + } catch (Exception e) { + System.out.println("Error using JavaScript method: " + e.getMessage()); + } + + } finally { + // Close the browser + driver.quit(); + } + } +} \ No newline at end of file diff --git a/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameEdge.java b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameEdge.java new file mode 100644 index 0000000..b845274 --- /dev/null +++ b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameEdge.java @@ -0,0 +1,82 @@ +package org.example.enhancementseleniumscripts.GetDownloadedFilePathUsingFileName; + +import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.edge.EdgeDriver; + +import java.time.Duration; + +public class GetDownloadPathUsingFileNameEdge { + + public static void main(String[] args) { + + WebDriver driver = new EdgeDriver(); + String targetFileName = "simple (2).txt"; // Replace with your desired file name + + try { + // Download the file twice to ensure it's present in downloads and to trigger the download twice + + String downloadURL = "https://drive.usercontent.google.com/download?id=1szYMoC4D-I5uZqiovJ5IOSwh8jVOVpmK&export=download&authuser=0&confirm=t&uuid=770e07f0-0783-4b5c-9c09-b36741378e9f&at=AEz70l58Um2in724t0ss8cJtPG7k:1740249548154"; + + driver.get(downloadURL); //Initiate the download + Thread.sleep(3000); //Wait for download to start + + driver.get(downloadURL); //Initiate the download again to see the downloaded history + + Thread.sleep(3000); //Wait for download to start + driver.get("edge://downloads/all"); + + Thread.sleep(Duration.ofSeconds(10).toMillis()); + + try { + String filePath = (String) ((JavascriptExecutor) driver).executeScript( + "function getDownloadedFilePath(targetFileName) {" + + " 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;" + + " }" + + "" + + " const latestFilePath = getLatestDownloadedFilePath();" + + " if (latestFilePath && latestFilePath.includes(targetFileName)) {" + + " return latestFilePath;" + + " }" + + " return null;" + + "}" + + "return getDownloadedFilePath(arguments[0]);", targetFileName); + + if (filePath != null && !filePath.isEmpty()) { + System.out.println("File path for " + targetFileName + ": " + filePath); + } else { + System.out.println("File " + targetFileName + " not found in downloads."); + } + } 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(); + } + } +} \ No newline at end of file diff --git a/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameFirefox.java b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameFirefox.java new file mode 100644 index 0000000..87b7b8b --- /dev/null +++ b/GetDownloadedFilePathUsingFileName/GetDownloadPathUsingFileNameFirefox.java @@ -0,0 +1,77 @@ +package org.example.enhancementseleniumscripts.GetDownloadedFilePathUsingFileName; + +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 GetDownloadPathUsingFileNameFirefox { + + public static void main(String[] args) { + // Initialize the WebDriver + WebDriver driver = new FirefoxDriver(); + + // Define the file name to search for + String fileNameToSearch = "simple(13).txt"; + + try { + // Navigate to the sample files page + driver.navigate().to("https://sample-files.com/documents/txt/"); + // Click the download button for the desired file + WebElement element = driver.findElement(By.xpath("//*[@id=\"post-178\"]/div/div/p[3]/a")); + element.click(); + // Navigate to the Firefox downloads page + Thread.sleep(3000); + + element.click(); + + driver.get("about:downloads"); + + // Wait for the downloads to appear + Thread.sleep(Duration.ofSeconds(10).toMillis()); + + // Execute the JavaScript function to get the file path + String filePath = (String) ((JavascriptExecutor) driver).executeScript( + "function getDownloadedFilePath(fileName) {" + + " const richListBox = document.querySelector('richlistbox#downloadsListBox');" + + " if (!richListBox) {" + + " return 'richlistbox element not found';" + + " }" + + " const downloadItems = richListBox.querySelectorAll('richlistitem');" + + " for (let i = 0; i < downloadItems.length; i++) {" + + " const downloadItem = downloadItems[i];" + + " const description = downloadItem.querySelector('.downloadTarget');" + + " if (description && description.getAttribute('value') === fileName) {" + + " const downloadTypeIcon = downloadItem.querySelector('image.downloadTypeIcon');" + + " if (downloadTypeIcon) {" + + " const filePath = downloadTypeIcon.getAttribute('src');" + + " if (filePath) {" + + " return filePath.split('?')[0].replace('moz-icon://', '');" + + " }" + + " }" + + " }" + + " }" + + " return 'file name not found';" + + "}" + + "return getDownloadedFilePath(arguments[0]);", fileNameToSearch); + + // Print the result + System.out.println("File path: " + filePath); + + if (filePath != null && !filePath.isEmpty() && !filePath.equals("file name not found")) { + System.out.println("Latest file path: " + filePath); + } else { + System.out.println("File name not found."); + } + } catch (Exception e) { + System.err.println("An error occurred: " + e.getMessage()); + e.printStackTrace(); + } finally { + // Close the WebDriver + driver.quit(); + } + } +} \ No newline at end of file