请帮帮我。我想创建新文件夹并保存其中的selenium的屏幕截图。
我想,当我点击按钮xxx_1时,将自动创建文件夹,其中包含我在 txt_Box1 和当前日期中输入的文字。 文件夹应该是这样的:
Test_test2_18_test3-test4_test5_test_11-JUL-2017
这是我的代码
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);
}
答案 0 :(得分:3)
您不能在DateTime
path
中使用.ToString()
,因为DateTime
上 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);
的默认实施将包含无效字符。使用格式说明符:
textBox1.Text
请注意,如果< > :
包含无效的路径字符,例如h1
,那么您将获得另一个例外。