【发布时间】:2015-09-15 15:13:33
【问题描述】:
我面临元素等待/呈现/可点击的随机行为问题。我已经使用下面的逻辑来等待该元素,但它有时会起作用,而其他时候则不起作用。有人可以帮助我如何用元素等待的通用/标准方式解决这个问题。
问题/要求:加载网页时,我必须等待一个元素出现/可点击。我正在使用 Selenium Web 驱动程序 2.44.0 和 Firefox 33.0.3
使用的逻辑:
package com.ericsson.testing.automation.framework.ui.common;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotVisibleException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.UnhandledAlertException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;
public class Test {
public static WebDriver driver;
public static long timeOut = 180000;
/** The poll time. */
public int pollTime = 100;
public void isElementClickable(WebDriver driver, WebElement webelement,
long timeOutForEachElement) {
try {
WebDriverWait wait = new WebDriverWait(driver,
timeOutForEachElement);
wait.until(ExpectedConditions.elementToBeClickable(webelement));
} catch (UnhandledAlertException ex) {
// Some debug logging info
}
}
// Senario 2
public void waitTillElementPresent(WebElement webelement) {
System.out.println("Before Fluent Wait");
System.out.println("Xpath = " + webelement);
FluentWait<WebElement> fluentWait = new FluentWait<WebElement>(
webelement);
fluentWait.pollingEvery(pollTime, TimeUnit.MILLISECONDS);
fluentWait.withTimeout(timeOut, TimeUnit.MILLISECONDS);
System.out.println("After Fluent Wait");
fluentWait.until(new Function<WebElement, Boolean>() {
public Boolean apply(WebElement webelement) {
try {
System.out.println("inside isDisplayed check");
return webelement.isDisplayed();
} catch (NoSuchElementException ex) {
System.out.println("Inside NoSuchElementException");
return false;
} catch (ElementNotVisibleException ex) {
System.out.println("Inside ElementNotVisibleException");
return false;
} catch (StaleElementReferenceException ex) {
System.out.println("Inside StaleElementRefException");
return false;
} catch (UnhandledAlertException ex) {
System.out.println("Inside UnhandledAlertException");
// Some logic for debug logging
return false;
}
}
});
}
// Senario 3
public void clickOnElement(String element) {
while (true) {
driver.findElement(By.xpath(element)).click();
System.out.println("Trying to click on element" + element);
break;
}
System.out.println("Clicked on element" + element);
}
/*
* private boolean isElementPresent(String element) {
* int myLink
* =driver.findElements(By.xpath(element)).size();
* if (myLink != 0) return true;
* else return false; }
*/
// Senario 4
public void waitTillElementisClicked(String element) {
boolean flag = true;
while (flag == true) {
// driver.findElement(By.xpath(element)).click();
// flag=driver.findElement(By.xpath(element)).isSelected();
driver.findElement(By.xpath(element)).click();
// driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
if (driver.findElement(By.xpath(element)).isSelected()) {
flag = false;
System.out.println("element already clicked");
}
// driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
System.out.println("trying to click the element");
}
System.out.println("Clicking the element");
}
}
【问题讨论】:
标签: firefox selenium selenium-webdriver automation webdriver