捕获字符串并将其作为 List<string> 传递给参数

Capture strings and pass it into an argument as a List<string>

我在 try catch 中有几个字符串使用相同的变量 location

try
{
    IWebElement bi7_reportHolder = ieDriver.FindElement(By.ClassName("iframe-qlik-view"));
    //Check if Pop-up is present
    location = ssLocation["SaveScreenShotLocation"] + "bi7_ScreenShot.png";
    CheckPopUpAndTakeScreenShot(ieDriver, executor, location);
}
catch (System.Exception e)
{
    Console.WriteLine(e.Message);
}


//Open bi8 Report - Concentration Risk
ieDriver.Navigate().GoToUrl("https://insightsmp.accenture.com/reports/9615a4f8-765f-4bed-9ccd-f8fa7cf3cbae/");
Thread.Sleep(60000);
wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
try
{
    IWebElement bi8_reportHolder = ieDriver.FindElement(By.Id("reportParent"));
    //Check if Pop-up is present
    location = ssLocation["SaveScreenShotLocation"] + "bi8_ScreenShot.png";
    CheckPopUpAndTakeScreenShot(ieDriver, executor, location);
}
catch (System.Exception e)
{
    Console.WriteLine(e.Message);
}

//Open Detailed Reporting
ieDriver.Navigate().GoToUrl("https://insightsmp.accenture.com/detailedReporting/");
Thread.Sleep(60000);
wait.Until(d => ((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").Equals("complete"));
try
{
    IWebElement dr_reportHolder = ieDriver.FindElement(By.Id("search-bar"));
    //Check if Pop-up is present
    location = ssLocation["SaveScreenShotLocation"] + "DR_ScreenShot.png";
    CheckPopUpAndTakeScreenShot(ieDriver, executor, location);
}
catch (System.Exception e)
{
    Console.WriteLine(e.Message);
}

我需要捕获所有这些字符串并将其作为 List 参数传递到我正在调用的方法中,以便我可以在该方法中使用它们。

List<string> screenshotLocation = location.ToString();

SendEmail(Application, screenshotLocation, bi6Accessible, bi7Accessible, bi8Accessible, detailedReportingAccessible);

我应该怎么做?

在您的第一个 try 语句上方,初始化您的列表。

List<string> screenshotLocation = new List<string>();

然后,每次设置 location 变量时,将其添加到该列表中,就像这样。

location = ssLocation["SaveScreenShotLocation"] + "bi7_ScreenShot.png";
screenshotLocation.Add(location);
//other code

然后当我调用您的 SendEmail 方法时,列表已准备好添加值。你可以像拥有它一样称呼它。 (假设其他变量也正确)