如何迭代IWebElements列表并存储在数组中然后在控制台c中打印#

时间:2017-12-11 18:29:04

标签: c# arrays linq selenium

我正在学习c#并试图理解数组。我在selenium c#中实际使用数组并遇到一种情况,我无法迭代将它们的值存储到数组中。我的意图是,我想存储数组中的所有价格然后与for循环一起在控制台中逐个迭代。

namespace OneDimensionalarray

{

class Program
{
    static void Main(string[] args)
    {

        IWebDriver driver = null;

        driver = new ChromeDriver();

        driver.Manage().Window.Maximize();

        driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);

        driver.Navigate().GoToUrl("http://www.shopclues.com/");  

        driver.FindElement(By.XPath("//input[@id='autocomplete']")). SendKeys("Lenovo");

        driver.FindElement(By.XPath("//span[@id='search']/a")).Click();

        IList<IWebElement> mobileprice = driver.FindElements(By.XPath("//span[@class='p_price']"));


 //I want to store mobiles prices into an array and then print in the console.

        int price = mobileprice.Count;

        int[] prices = new int[price];

        for (int i = 0; i < prices.Length; i++)
        {
            Console.WriteLine(price);
            Console.Read();
        }

    }

    }
}

2 个答案:

答案 0 :(得分:0)

你正在创建数组,但在通过它之前没有用任何东西填充它。

尝试(这不使用数组 - 不需要它......)

//I want to store mobiles prices into an array and then print in the console.
foreach (IWebElement priceElement in mobileprice )
{
  Console.WriteLine(priceElement.getText());
  Console.Read();
}

答案 1 :(得分:0)

要将所有价格存储在数组中,然后使用for循环在控制台中逐个迭代,您可以使用以下代码块:

IList<IWebElement> mobileprice = driver.FindElements(By.XPath("//span[@class='p_price']"));
foreach (IWebElement price in mobileprice )
    Console.WriteLine(price.GetAttribute("innerHTML"));