3

please help me. I would like to create new folder and save a screenshots from selenium therein.

I want, when I click the button xxx_1, folder will automatically be created with text which I enter in txt_Box1 and currently date. Folder should be looks like that:

Test_test2_18_test3-test4_test5_test_11-Jul-2017

Here's my code

private void xxx_1(object sender, EventArgs e)
{
    string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 
           + "C:/xxx/xxx" + "_" + textBox1 + "_" + "xxx_xxx_xx_" + DateTime.Now;

    if (!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

    //string path = @"C:\\xxx\xxx" + "_" + textBox1 + "_" + "xxx_xxx_xxx_" + DateTime.Now; 

    String xxx= "https://xxx.xxx.xxx";

    IWebDriver driver_xx = new ChromeDriver();
    driver_xx.Navigate().GoToUrl(xxx);
    driver_xx.FindElement(By.Id("xxx")).SendKeys("xxx");
    driver_xx.FindElement(By.Id("xx")).SendKeys("xxx");
    driver_xx.FindElement(By.Id("xx")).Click();
    Thread.Sleep(3000);

    Screenshot ss_xx = ((ITakesScreenshot)driver_xx).GetScreenshot();
    ss_xx.SaveAsFile("How to save the screenshots in new created folder??", OpenQA.Selenium.ScreenshotImageFormat.Jpeg);
}
Bas
  • 4,423
  • 8
  • 36
  • 53
Freeezer789
  • 107
  • 1
  • 5
  • What's the value of `path` if you debug? To save the file is it not something like `ss_xx.SaveAsFile(Path.Combine(path, filename), OpenQA.Selenium.ScreenshotImageFormat.Jpeg);`? – Equalsk Sep 21 '17 at 14:32
  • It's working if I use this code string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\xxx"; Directory.CreateDirectory(path); Screenshot ss_xxx = ((ITakesScreenshot)driver_xxx).GetScreenshot(); ss_xxx.SaveAsFile(Path.Combine(path,"xxx_environment_version.Jpeg"), OpenQA.Selenium.ScreenshotImageFormat.Jpeg); driver_xxx.Close(); Folder is creating in appdata/roaming, but I want that folder should be create on C:\\ or Desktop with text //string path = @"C:\\xxx\xxx" + "_" + textBox1 + "_" + "xxx_xxx_xxx_" + DateTime.Now; – Freeezer789 Sep 21 '17 at 14:50
  • I'm confused, you want it on the Desktop but fetch the Application Data folder, why not just get the Desktop folder then!? – Equalsk Sep 21 '17 at 14:58
  • Yes, You have right but how can I create this folder with specific name? string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "xx/xx" + "_" + textBox1 + "_" + "xx_xxx_xxx_" + DateTime.Now; Directory.CreateDirectory(path); When I try this above code I have an error: System.NotSupportedException: 'The given path's format is not supported.' – Freeezer789 Sep 21 '17 at 15:03
  • Possible duplicate of [If a folder does not exist, create it](https://stackoverflow.com/questions/9065598/if-a-folder-does-not-exist-create-it) – JeffC Sep 21 '17 at 18:00
  • The path may be invalid. Please print the value of `path` generated and make sure it is valid and no symbols or characters which are not allowed – Tarun Lalwani Sep 22 '17 at 07:07

1 Answers1

4

You can't use a DateTime in your path like that as the default implementation of .ToString() on a DateTime will contain invalid characters. Use a format specifier:

 string path = Path.Combine(
                   Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
                   "xx\\xx",
                   textBox1.Text,
                   "xx_xxx_xxx_",
                   DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") // This will show '21-09-2017 16-11-15'
               ); 

Directory.CreateDirectory(path); 

Be careful that if textBox1.Text contains invalid path characters such as < > : then you'll get another exception.

Equalsk
  • 7,954
  • 2
  • 41
  • 67