我在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和其他东西。 我的代码在这一刻有问题。
答案 0 :(得分:0)
您可以尝试这样的事情:
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);
}
});
}