下面列出了org.openqa.selenium.WebDriverException#getMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Check whether exception indicates a 'stale element', not all drivers throw the exception one would expect...
* @param e exception caught
* @return true if exception indicated the element is no longer part of the page in the browser.
*/
public boolean isStaleElementException(WebDriverException e) {
boolean result = false;
if (e instanceof StaleElementReferenceException) {
result = true;
} else {
String msg = e.getMessage();
if (msg != null) {
result = msg.contains("Element does not exist in cache") // Safari stale element
|| msg.contains("unknown error: unhandled inspector error: {\"code\":-32000,\"message\":\"Cannot find context with specified id\"}") // chrome error
|| msg.contains("Error: element is not attached to the page document") // Alternate Chrome stale element
|| msg.contains("can't access dead object"); // Firefox stale element
}
}
return result;
}
/**
* Executes Javascript in browser. If script contains the magic variable 'arguments'
* the parameters will also be passed to the statement. In the latter case the parameters
* must be a number, a boolean, a String, WebElement, or a List of any combination of the above.
* @link http://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/JavascriptExecutor.html#executeScript(java.lang.String,%20java.lang.Object...)
* @param script javascript to run.
* @param parameters parameters for the script.
* @return return value from statement.
*/
public static Object executeScript(JavascriptExecutor jse, String script, Object... parameters) {
Object result;
try {
result = jse.executeScript(script, parameters);
} catch (WebDriverException e) {
String msg = e.getMessage();
if (msg != null && msg.contains("Detected a page unload event; script execution does not work across page loads.")) {
// page reloaded while script ran, retry it once
result = jse.executeScript(script, parameters);
} else {
throw e;
}
}
return result;
}
public T apply(WebDriver driver, String[] args) {
try {
switch (args.length) {
case 0:
return handleSeleneseCommand(driver, null, null);
case 1:
return handleSeleneseCommand(driver, args[0], null);
case 2:
return handleSeleneseCommand(driver, args[0], args[1]);
default:
throw new SeleniumException("Too many arguments! " + args.length);
}
} catch (WebDriverException e) {
throw new SeleniumException(e.getMessage(), e);
}
}
/**
* Check whether exception indicates connection with webdriver is lost.
* @param e exception caught
* @return true if exception indicated we can no longer communicate with webdriver.
*/
public boolean exceptionIndicatesConnectionLost(WebDriverException e) {
boolean result = e.getCause() instanceof SocketException;
if (!result && e.getMessage() != null) {
result = e.getMessage().contains("java.net.SocketException")
|| e.getMessage().contains("java.net.ConnectException");
}
return result;
}
@Override
public WebDriver context(String name) {
checkNotNull(name, "Must supply a context name");
try {
execute(DriverCommand.SWITCH_TO_CONTEXT, ImmutableMap.of("name", name));
return this;
} catch (WebDriverException e) {
throw new NoSuchContextException(e.getMessage(), e);
}
}
/**
* Configures and returns a new {@link XpiDriverService} using the default configuration. In
* this configuration, the service will use the firefox executable identified by the
* {@link FirefoxDriver.SystemProperty#BROWSER_BINARY} system property on a free port.
*
* @return A new XpiDriverService using the default configuration.
*/
public static XpiDriverService createDefaultService() {
try {
return new Builder().build();
} catch (WebDriverException e) {
throw new IllegalStateException(e.getMessage(), e.getCause());
}
}
@Override
public ClickResult click(WebElement element, Optional<Action> defaultAlertAction)
{
ClickResult clickResult = new ClickResult();
if (element != null)
{
WebDriver webDriver = getWebDriver();
WebElement page = webDriver.findElement(BODY_XPATH_LOCATOR);
try
{
javascriptActions.scrollElementIntoViewportCenter(element);
element.click();
afterClick(clickResult, page, webDriver, defaultAlertAction);
}
catch (WebDriverException webDriverException)
{
String message = webDriverException.getMessage();
if (message.contains("is not clickable at point"))
{
try
{
if (webDriverManager.isTypeAnyOf(WebDriverType.CHROME)
&& message.contains(". Other element would receive the click"))
{
javascriptActions.click(element);
}
else
{
element.click();
}
afterClick(clickResult, page, webDriver, defaultAlertAction);
}
catch (WebDriverException e)
{
softAssert.recordFailedAssertion(COULD_NOT_CLICK_ERROR_MESSAGE + e);
}
}
else if (message.contains("timeout: Timed out receiving message from renderer"))
{
softAssert.recordFailedAssertion(COULD_NOT_CLICK_ERROR_MESSAGE + webDriverException);
}
else if (message.contains("Timed out waiting for page to load"))
{
afterClick(clickResult, page, webDriver, defaultAlertAction);
}
else
{
throw webDriverException;
}
}
}
return clickResult;
}