diff --git a/GetLatestDownloadedFilePath/GetLatestDownloadPathChrome.java b/GetLatestDownloadedFilePath/GetLatestDownloadPathChrome.java new file mode 100644 index 0000000..7f72fca --- /dev/null +++ b/GetLatestDownloadedFilePath/GetLatestDownloadPathChrome.java @@ -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(); + } + } +} + + + + + + diff --git a/GetLatestDownloadedFilePath/GetLatestDownloadPathEdge.java b/GetLatestDownloadedFilePath/GetLatestDownloadPathEdge.java new file mode 100644 index 0000000..837d08d --- /dev/null +++ b/GetLatestDownloadedFilePath/GetLatestDownloadPathEdge.java @@ -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(); + } + } +} diff --git a/GetLatestDownloadedFilePath/GetLatestDownloadPathFirefox.java b/GetLatestDownloadedFilePath/GetLatestDownloadPathFirefox.java new file mode 100644 index 0000000..c179e80 --- /dev/null +++ b/GetLatestDownloadedFilePath/GetLatestDownloadPathFirefox.java @@ -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(); + } + } +} \ No newline at end of file