如何在Arrays Selenium中添加IList <iwebelement>

时间:2015-05-15 08:36:57

标签: arrays excel selenium ilist

我是C#selenium的新手。

这是示例代码,工作正常,我知道它不是标准的编码方式,但我不知道这个实例的标准代码是什么。

我必须从每个字符串中提取5000-10000个元素,所以有任何最快和最标准的代码,我认为我的解决方案太慢了。

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 个答案:

答案 0 :(得分:0)

试试这个,它应该有希望加快处理速度。

也许尝试添加StopWatch()课程来计算与我相比的实施时间?

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

答案 1 :(得分:0)

请使用你的代码来解决这个问题     我得到这个错误,     System.Runtime.InteropServices.COMException未被用户代码处理       的HResult = -2146827284       消息=来自HRESULT的异常:0x800A03EC       来源=“”       错误码= -2146827284       堆栈跟踪:            at System.RuntimeType.ForwardCallToInvokeMember(String memberName,BindingFlags flags,Object target,Int32 [] aWrapperTypes,MessageData&amp; msgData)            在Microsoft.Office.Interop.Excel.Range.set__Default(Object RowIndex,Object ColumnIndex,Object value)            at CDP_Win.Form1。&lt;&gt; c__DisplayClass8.b__2(Int32 i)in e:\ MyProjects \ CDP_Win - Copy \ CDP_Win \ Form1.cs:第115行            在System.Threading.Tasks.Parallel。&lt;&gt; c__DisplayClassf`1.b__c()       的InnerException:     @Jamie Rees,@ Mark Rowlands