java.io.FileNotFoundException: c:\screenshot.png (Access is denied) error while trying to paste the screenshot to a system folder

人走茶凉 提交于 2020-02-25 16:56:28

问题


public class Automate1 { 
    public static void main(String[] args) throws IOException { 

        System.setProperty("webdriver.chrome.driver","C:\Users\DELL\Downloads\chromedriver.exe"); 
        WebDriver driver=new ChromeDriver(); 
        driver.manage().window().maximize(); 
        driver.manage().deleteAllCookies(); 
        driver.get("https://www.amazon.in"); 
        Select SC=new Select(driver.findElement(By.xpath("//select[@id='searchDropdownBox']"))); 
        //Selecting an option from drop down by using Select class
        SC.selectByVisibleText("Books"); driver.findElement(By.xpath("//input[@id=\"twotabsearchtextbox\"]")).sendKeys("The God Father"); 
        driver.findElement(By.xpath("//input[@class=\"nav-input\"]")).click(); 
        JavascriptExecutor Scroll = ((JavascriptExecutor) driver); 
        //For Scrolling Functionality 
        Scroll.executeScript("scroll(0,800)"); 
        //Scrolling Up
        Scroll.executeScript("scroll(0,-200)"); 
        //Scrolling down 
        driver.findElement(By.linkText("The Godfather")).click(); 
        ArrayList Handles=new ArrayList(driver.getWindowHandles()); 
        driver.switchTo().window(Handles.get(1)); 
        Select Q=new Select(driver.findElement(By.id("quantity"))); 
        Q.selectByIndex(1); 
        driver.findElement(By.id("add-to-cart-button")).click(); 
        driver.close(); 
        driver.switchTo().window(Handles.get(0)); 
        TakesScreenshot scrShot =((TakesScreenshot)driver); 
        File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); 
        File DestFile=new File("c:\screenshot.png"); 
        FileUtils.copyFile(SrcFile, DestFile);

Error:

Exception in thread "main" java.io.FileNotFoundException: c:\screenshot.png (Access is denied)

回答1:


When you create a new File you need to provide the absolute path, in terms of root directory (i.e. C:), sub-directory and the filename.

Effectively, you need to change the line as follows:

File DestFile=new File("C:\\some_sub_directory\\screenshot.png");



回答2:


Replace

File DestFile=new File("c:\screenshot.png"); 

with

File DestFile=new File("c:/screenshot/screenshot.png"); 


来源:https://stackoverflow.com/questions/59144607/java-io-filenotfoundexception-c-screenshot-png-access-is-denied-error-while

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!