簡體   English   中英

使用 selenium C# 查找和替換文本文件中的數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM