0

Why am I getting this error message? It is this line in my code that it is complain about:

website = x1range.Cells[i][1].value2;

I am getting

Error CS0021 Cannot apply indexing with [] to an expression of type 'object'

This is my code:

using System; using System.Threading; using OpenQA.Selenium; using
OpenQA.Selenium.Chrome; using excel =
Microsoft.Office.Interop.Excel;

namespace Create Editions
{
    class Program
    {
        static void Main(string[] args)
        {
            excel.Application x1app = new excel.Application();
            excel.Workbook x1workbook = x1app.Workbooks.Open(@"D:\DATA\CreateEditions.xlsx");
            excel._Worksheet x1worksheet = (excel._Worksheet)x1workbook.Sheets[1];
            excel.Range x1range = x1worksheet.UsedRange;
            string website;
    
            for(int i=1; i<=3; i++)
            {
               website = x1range.Cells[i][1].value2;
               IWebDriver driver = new ChromeDriver();
               driver.Navigate().GoToUrl(website);
            }
       }
   }
}
Luke Woodward
  • 63,336
  • 16
  • 89
  • 104

1 Answers1

0

x1range.Cells[i] returns an object which is the return type of the Range indexer. So please convert the type of x1range.Cells[i] to Range. Reference

website = (string)((Range)x1range.Cells[i])[1].value2;
dear_vv
  • 2,350
  • 1
  • 4
  • 13