简体   繁体   English

如何使用 Selenium RemoteWebDriver 删除下载的文件?

[英]How can I delete a downloaded file using Selenium RemoteWebDriver?

I am writing a test to check that a file can be downloaded from a particular web page and I want it to be able to run both locally and remotely (ie on a node via Selenium grid).我正在编写一个测试来检查文件是否可以从特定的网页下载,我希望它能够在本地和远程运行(即通过 Selenium 网格在节点上运行)。 Before anyone links me to the 'do you really need to download the file?'在任何人将我链接到“您真的需要下载文件吗?”之前article, I have already managed to download and check the file, I just need a way of deleting it after the test has completed.文章,我已经设法下载并检查了文件,我只需要一种在测试完成后删除它的方法。 Just calling File.delete();只需调用File.delete(); or similar will only work locally (as far as I'm aware) so I can't use that to delete the file from the node machine.或类似的只能在本地工作(据我所知),所以我不能用它来从节点机器上删除文件。 I'm aware of the class org.openqa.selenium.io.TemporaryFileSystem however I can't find any instructions for how to use it.我知道类org.openqa.selenium.io.TemporaryFileSystem但是我找不到有关如何使用它的任何说明。

Can anyone offer a better solution than 'just run a script on the node machine to delete the file'?谁能提供比“仅在节点机器上运行脚本以删除文件”更好的解决方案? Thanks!谢谢!

You can make the download folder shared.您可以将下载文件夹设为共享。 \\youruser\\downloads after that you can pass this path to the File.Delete(); \\youruser\\downloads 之后,您可以将此路径传递给 File.Delete(); and it will delete the desired files.它将删除所需的文件。

This Worked for me这对我有用

try 
{
if ((new File("Path")).delete()) {
                System.out.println("Pass");     
            } else {
                System.out.println("Failed");
            }

          } catch (Exception ex) {
            ex.printStackTrace();
          }
   ----------simply use this code for delete file in any folder-------------------       
   File file = new File("C:\\Users\\Updoer\\Downloads\\Inspections.pdf"); 
   if(file.delete())
   System.out.println("file deleted");

The Below Code will sequentially delete all the files from folder下面的代码将依次删除文件夹中的所有文件

    File path = new File("Path of Folder");
    File[] files = path.listFiles();
    for (File file : files) {
        System.out.println("Deleted filename :"+ file.getName());
        file.delete();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Selenium RemoteWebDriver中设置浏览器版本? - How can I set the browser version in Selenium RemoteWebDriver? 我可以使用Selenium在浏览器堆栈中验证下载的文件吗 - Can i verify downloaded file in browser stack using selenium 将Selenium 2 RemoteWebDriver与ChromeDriver结合使用 - Using Selenium 2 RemoteWebDriver with ChromeDriver 使用ThreadLocal的FluentWait硒<RemoteWebDriver> - FluentWait selenium using ThreadLocal<RemoteWebDriver> Selenium:如何判断RemoteWebDriver.findElements(By)是否可以引发StaleElementReferenceException? - Selenium: How to tell if RemoteWebDriver.findElements(By) can throw StaleElementReferenceException at all? Selenium Grid:如何使用RemoteWebDriver和ChromeDriver最大化浏览器窗口 - Selenium Grid: how to maximize browser window using RemoteWebDriver and ChromeDriver 如何使用 Selenium RemoteWebDriver 设置 HTTP_PROXY? - How to set HTTP_PROXY using Selenium RemoteWebDriver? 如何使用Selenium Webdriver获得下载文件的文件大小? - How to get the file size of a downloaded file using Selenium Webdriver? 在公司代理(Java)后面使用Selenium RemoteWebDriver - Using Selenium RemoteWebDriver behind corporate proxy (Java) Selenium IEDriverServer在使用RemoteWebDriver时不发送密钥 - Selenium IEDriverServer not sending keys when using RemoteWebDriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM