Freeezer789
Freeezer789

Reputation: 107

How to create a folder and save screenshots therein

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);
}

Upvotes: 3

Views: 2158

Answers (1)

Equalsk
Equalsk

Reputation: 8224

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.

Upvotes: 4

Related Questions