首页 > 解决方案 > 捕获字符串并将其作为列表传递给参数

问题描述

我在 try catch 中有几个字符串,它们使用相同的变量位置

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

我该怎么做?

标签: c#stringseleniumarraylistc#-4.0

解决方案


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

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

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

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

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


推荐阅读