[英]Find and replace the data in text file using selenium C#
我有一個要上傳到 UI 中的文本文件。 每次測試運行前都需要更新文本文件。 我需要在我的 Selenium C# 腳本中捕獲它。
下面是數據格式:
0 01CBA Sou 301500PAYMEN72 080416
1062-000 14138 130000000012ROSY DEBITFROM
080416
是日期。 每次腳本運行時,我都需要用系統日期替換它。
我怎樣才能獲得日期的位置並替換它。
我可以使用正則表達式來替換日期:
// load the file
string text = File.ReadAllText(@"C:\yourfile.txt");
// replace the date
string text2 = Regex.Replace(text, @"\b080416\b", DateTime.Now.ToString("ddmmyy"));
// save the updated text to a temp file
string tempPath = string.Format("{0}myfile-{1:X}.txt", Path.GetTempPath(), DateTime.Now.Ticks);
File.WriteAllText(tempPath, text2);
// upload the file
driver.FindElementByCssSelector("input[type=file]").SendKeys(tempPath);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.