How to add IList<IWebElement> in Arrays Selenium

1k Views Asked by At

I am new to both C# and selenium.

This is the sample code, it is working fine, I know it's not a standard way of coding but I don't know what is the standard code for this instance.

I have to extract 5000-10000 elements from each string, so is there any fastest and standard code for this, I think my solution will be too slow.

IList<IWebElement> ARNum = driver.FindElement(By.XPath("sumXpath1"));
IList<IWebElement> MPNum = driver.FindElement(By.XPath("sumXpath2"));
IList<IWebElement> MGNum = driver.FindElement(By.XPath("sumXpath3"));

int RowARNum=1;
foreach(IWebElement artNum in ARNum) {//for First Column
    RowARNum++;
    XLWorkSheet.Cells(RowARNum, 1) = artNum.Text;
}

int RowMPNum =1;
foreach(IWebElement mpNum in MPNum){ //for Second Column
    RowMPNum++;
    XLWorkSheet.Cells(RowMPNum, 1) = mpNum.Text;
}

int RowMGNum =1;
foreach(IWebElement mgNum in MGNum){ //for Third Column
    RowMGNum++;
    XLWorkSheet.Cells(RowMGNum, 1) = mgNum.Text;
}
2

There are 2 best solutions below

9
Jamie Rees On

Try this, It should hopefully speed up the processing.

Maybe try and add the StopWatch() class to time your implementation compared to mine?

        IList<IWebElement> ARNum = driver.FindElements(By.XPath("sumXpath1"));
        IList<IWebElement> MPNum = driver.FindElements(By.XPath("sumXpath2"));
        IList<IWebElement> MGNum = driver.FindElements(By.XPath("sumXpath3"));

        Parallel.For(0, ARNum.Count(), i =>
        {
            XLWorkSheet.Cells(i, 1) = ARNum[i].Text;
        });

        Parallel.For(0, MPNum.Count(), i =>
        {
            XLWorkSheet.Cells(i, 1) = MPNum[i].Text;
        });

        Parallel.For(0, MGNum.Count(), i =>
        {
            XLWorkSheet.Cells(i, 1) = MGNum[i].Text;
        });
0
Tester Cs On

pls chk this while using ur code Iam getting this error, System.Runtime.InteropServices.COMException was unhandled by user code HResult=-2146827284 Message=Exception from HRESULT: 0x800A03EC Source="" ErrorCode=-2146827284 StackTrace: at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData) at Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex, Object ColumnIndex, Object value) at CDP_Win.Form1.<>c__DisplayClass8.b__2(Int32 i) in e:\MyProjects\CDP_Win - Copy\CDP_Win\Form1.cs:line 115 at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.b__c() InnerException: @Jamie Rees , @Mark Rowlands