Java selenium WebDriver-如果加载时间过长或关闭浏览器并继续,如何重新加载页面?
我在Java中使用Selenium 2.45.0。我对facebook页面有问题。当页面加载时间过长时,程序在20秒后停止并中断程序 程序存在查找元素可选择的问题,这是结束工作程序 我使用了很多命令: driver.manage().timeouts().pageLoadTimeout(5000,TimeUnit.ms);//这不起作用Java selenium WebDriver-如果加载时间过长或关闭浏览器并继续,如何重新加载页面?,java,selenium,webdriver,Java,Selenium,Webdriver,我在Java中使用Selenium 2.45.0。我对facebook页面有问题。当页面加载时间过长时,程序在20秒后停止并中断程序 程序存在查找元素可选择的问题,这是结束工作程序 我使用了很多命令: driver.manage().timeouts().pageLoadTimeout(5000,TimeUnit.ms);//这不起作用 Set<String> winSet = driver.getWindowHandles(); List<String> winList
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);
}
这个代码怎么了?
这不是全部代码,而是代码的唯一元素。
我用while和其他东西。
我的代码在这一刻遇到了问题。您可以尝试以下方法:
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);
}
});
}
存在公共静态布尔窗口(WebDriver驱动程序、字符串标题、,
测试策略(TestPolicy)
{
迭代器句柄=driver.getWindowHandles().Iterator();
while(handles.hasNext())
{
字符串句柄=句柄.next();
if(handle==null)
{
继续;
}
尝试
{
WebDriver popup=driver.switchTo().window(句柄);
if(popup.getTitle().equalsIgnoreCase(标题))
{
返回true;
}
}
捕获(无捕获)
{
//System.out.println(“找不到窗口句柄”+
//手柄);
}
}
返回false;
}
public static void waituntlwindowexists(最终WebDriver驱动程序,
最终字符串(windowName,最终测试策略TestPolicy)
{
新的WebDriverWait(驱动程序,12).until(新谓词()
{
@凌驾
公共布尔应用(WebDriver arg0)
{
返回OLCommon.windowExists(驱动程序、windowName、testPolicy);
}
});
}
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);
}
});
}