2017-04-20 77 views
0

我正在通过使用硒dll's网页刮。但我刮脸时记录清单的问题。如果我使用调试器,它将提取所有记录,但是如果禁用调试器并运行应用程序,它有时会显示较少的记录或某个时间显示没有记录。我想知道是否有任何方法可以知道页面是否已完全加载。硒刮在c#

driver.FindElementsByClassName( “搜索结果 - GridView的项目”)。ToList()

改变记录数。

      driver.FindElementsByClassName("search-result-gridview-item").ToList().ForEach(x => 
          { 
           objUPCProcess = new UPCProcessingModel(); 
           try 
           { 
            objUPCProcess.Description = x.FindElement(By.ClassName("prod-ProductTitle")).Text; 
            objUPCProcess.Price = x.FindElement(By.ClassName("Price")).Text; 
            listOfProductDetails.Add(objUPCProcess); 
            if (i == 0) 
            { 
             log.Item = objUPCProcess.Description; 
             i++; 
            } 
           } 
           catch (OpenQA.Selenium.NoSuchElementException ex) 
           { 
            try 
            { 
             objUPCProcess.Description = x.FindElement(By.ClassName("prod-ProductTitle")).Text; 
             objUPCProcess.Price = ex.Message; 
             listOfProductDetails.Add(objUPCProcess); 
            } 
            catch 
            { 
             try 
             { 
              objUPCProcess.Price = x.FindElement(By.ClassName("Price")).Text; 
              objUPCProcess.Description = ex.Message; 
              listOfProductDetails.Add(objUPCProcess); 
             } 
             catch 
             { 
              objUPCProcess.Description = ex.Message; 
              objUPCProcess.Price = ex.Message; 
              log.Message = ex.Message; 
              listOfProductDetails.Add(objUPCProcess); 
              log.Status = "Error"; 
             } 

            } 


           } 
          }); 
         } 

回答

0

看着这种情况下,我敢肯定,这是通过与实际填充记录AJAX请求同步问题(的webdriver和页面的工作在不同速度)引起的。这就是为什么

如果禁用调试并运行一段时间显示记录较少或有时在应用程序中显示没有记录

我还没有看到你的代码中的任何explicit waits。但是你可以实现一个辅助Utils方法,将检查

是尚未在页面完全加载与否

我用IScriptExecutor像这样:

public void WaitSecondsForNewPageToLoad(int maxWaitTimeInSeconds) 
{ 
    string state = string.Empty; 
    bool jQueryActive = true; 
    try 
    { 
     WebDriverWait wait = new WebDriverWait(TestCaseContext.Driver, 
      TimeSpan.FromSeconds(maxWaitTimeInSeconds)); 
     //Checks every 500 ms whether predicate returns true if returns exit otherwise keep trying till it returns true 
     wait.Until(d => 
     { 
      try 
      { 
       state = 
        ((IJavaScriptExecutor) TestCaseContext.Driver).ExecuteScript(
         @"return document.readyState").ToString(); 
       jQueryActive = 
        (bool)((IJavaScriptExecutor) TestCaseContext.Driver).ExecuteScript(
         @"return jQuery.active == 0"); 
       WindowsWhenSteps.WhenIFocusTheCurrentBrowserWindow(); 
      } 
      catch (InvalidOperationException) 
      { 
       //Ignore 
      } 
      return (state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || 
        state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase)) && 
        jQueryActive; 
     }); 
    } 
    catch (TimeoutException) 
    { 
     //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls 
     if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase)) 
      Assert.IsTrue(false); 
    } 
    catch (NullReferenceException) 
    { 
     //sometimes Page remains in Interactive mode and never becomes Complete, then we can still try to access the controls 
     if (!state.Equals("interactive", StringComparison.InvariantCultureIgnoreCase)) 
      Assert.IsTrue(false); 
    } 
    catch (WebDriverException) 
    { 
     if (TestCaseContext.Driver.WindowHandles.Count == 1) 
     { 
      TestCaseContext.Driver.SwitchTo().Window(TestCaseContext.Driver.WindowHandles[0]); 
     } 
     state = 
      ((IJavaScriptExecutor) TestCaseContext.Driver).ExecuteScript(
       @"return document.readyState").ToString(); 
     if (
      !(state.Equals("complete", StringComparison.InvariantCultureIgnoreCase) || 
       state.Equals("loaded", StringComparison.InvariantCultureIgnoreCase))) 
      Assert.IsTrue(false); 
    } 
} 

注:如果它看起来像一个开销,你可能会失去一些异常处理,但我的目标是赞成fu也是读者。