【问题标题】:Java selenium WebDriver- how to reload page if loading takes too long or close browser and go forward?Java selenium WebDriver-如果加载时间过长或关闭浏览器并继续前进,如何重新加载页面?
【发布时间】:2015-07-14 13:12:31
【问题描述】:

我使用 Java 中的 Selenium 2.45.0。我的 Facebook 页面有问题。当页面加载这么久时,程序在 20 秒后停止并中断程序。

程序在查找 elementToBeClickable 时出现问题,这是最终工作程序。

我使用了很多命令: driver.manage().timeouts().pageLoadTimeout(5000, TimeUnit.MILLISECONDS); // 这不起作用

Set<String> winSet = driver.getWindowHandles();
List<String> winList = new ArrayList<String>(winSet);
String newTab = winList.get(winList.size() - 1);
driver.switchTo().window(newTab);

sleep(1500);


try {
      driver.manage().timeouts().pageLoadTimeout(5000, TimeUnit.MILLISECONDS);
     try {
         driver.findElement(By.xpath("//span[@id='pagesHeaderLikeButton']/button")).click();
     } catch (Exception e) {
         System.out.println("This is not exist");
     }

} catch(Exception e) {
        System.out.println("no page");
        System.out.println(e);
    }

//当页面加载并在20秒后自动关闭时,不存在异常并且程序有刹车。

Thread.sleep(3000);
try {
    driver.close();
} catch (Exception e) {
    System.out.println("i cant close facebook");
}


// Allow time for the Like action to go through
sleep(1000);
// Close this window and switch back to the main one

try {
    driver.switchTo().window(windowHandle);
}
catch(Exception e) {
    System.out.println("cant back to main window");
}

这段代码有什么问题? 这不是所有的代码,而只是代码的元素。 我使用while和其他东西。 我的代码的这一刻有问题。

【问题讨论】:

    标签: java selenium webdriver


    【解决方案1】:

    你可以试试这样的:

    public static boolean windowExists(WebDriver driver, String title,
         TestPolicy testPolicy)
    {
      Iterator<String> handles = driver.getWindowHandles().iterator();
    
      while (handles.hasNext())
      {
         String handle = handles.next();
         if (handle == null)
         {
            continue;
         }
         try
         {
            WebDriver popup = driver.switchTo().window(handle);
            if (popup.getTitle().equalsIgnoreCase(title))
            {
               return true;
            }
         }
         catch (NoSuchWindowException e)
         {
            // System.out.println("Couldn't find the window handle " +
            // handle);
         }
      }
      return false;    
    }
    
    
    public static void waitUntilWindowExists(final WebDriver driver,
         final String windowName, final TestPolicy testPolicy)
    {
      new WebDriverWait(driver, 12).until(new Predicate<WebDriver>()
      {
         @Override
         public boolean apply(WebDriver arg0)
         {
            return OLCommon.windowExists(driver, windowName, testPolicy);
         }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 2011-07-08
      • 2015-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      • 1970-01-01
      相关资源
      最近更新 更多