Best Selenium code snippet using org.openqa.selenium.WebDriverException.addInfo
org.openqa.selenium.WebDriverExceptionThe WebDriver error - unknown error, happens when the driver tries to process a command and an unspecified error occurs.
The error can generally be isolated to the specific driver. It is a good practice to read the error message for any pointers on why the error occurred.
The error message shows that Selenium webdriver is not able to focus on element. generally, it happens due to incompatibility between Browser and Driver versions

1Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot focus element
2  (Session info: chrome=61.0.3163.100)
3  (Driver info: chromedriver=2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
4Command duration or timeout: 0 milliseconds
5Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
6System info: host: 'DWA7DEVOS00170', ip: '10.96.162.167', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_25'
7Driver info: org.openqa.selenium.chrome.ChromeDriver
Here are code snippets that can help you understand more how developers are using
Source:ANDDriverWait.java  
...118        // c.setTimeInMillis(t_end-t_start);119        // TRunnerLog.info("èæ¶: " + c.get(Calendar.MINUTE) + "å "+ c.get(Calendar.SECOND) + "ç§ " + c.get(Calendar.MILLISECOND) + " å¾®ç§");120        // TRunnerLog.endTestCase(Thread.currentThread().getStackTrace()[1].getMethodName());121        TimeoutException ex = new TimeoutException(message, lastException);122        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());123        if (driver instanceof RemoteWebDriver) {124            RemoteWebDriver remote = (RemoteWebDriver) driver;125            if (remote.getSessionId() != null) {126                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());127            }128            if (remote.getCapabilities() != null) {129                ex.addInfo("Capabilities", remote.getCapabilities().toString());130            }131            if (this.flag4Screen == false) {132            } else {133                ScreenShot(remote, "TimeOut : " + time + ".png", this.path4Log);134            }135        }136        throw ex;137    }138    protected RuntimeException timeoutException(String message, Throwable lastException, boolean flag4noscreen) {139        TimeoutException ex = new TimeoutException(message, lastException);140        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());141        if (driver instanceof RemoteWebDriver) {142            RemoteWebDriver remote = (RemoteWebDriver) driver;143            if (remote.getSessionId() != null) {144                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());145            }146            if (remote.getCapabilities() != null) {147                ex.addInfo("Capabilities", remote.getCapabilities().toString());148            }149        }150        throw ex;151    }152    protected RuntimeException timeoutExceptionNOScreen(String message, Throwable lastException) {153        TimeoutException ex = new TimeoutException(message, lastException);154        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());155        if (driver instanceof RemoteWebDriver) {156            RemoteWebDriver remote = (RemoteWebDriver) driver;157            if (remote.getSessionId() != null) {158                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());159            }160            if (remote.getCapabilities() != null) {161                ex.addInfo("Capabilities", remote.getCapabilities().toString());162            }163        }164        throw ex;165    }166}...Source:IOSDriverWait.java  
...124    }125    @Override126    protected RuntimeException timeoutException(String message, Throwable lastException) {127        TimeoutException ex = new TimeoutException(message, lastException);128        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());129        if (driver instanceof RemoteWebDriver) {130            RemoteWebDriver remote = (RemoteWebDriver) driver;131            if (remote.getSessionId() != null) {132                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());133            }134            if (remote.getCapabilities() != null) {135                ex.addInfo("Capabilities", remote.getCapabilities().toString());136            }137            if (this.flag4Screen == false) {138            } else {139                ScreenShot(remote, "TimeOut : " + time + ".png", this.path4Log);140            }141        }142        throw ex;143    }144    protected RuntimeException timeoutException(String message, Throwable lastException, boolean flag4noscreen) {145        TimeoutException ex = new TimeoutException(message, lastException);146        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());147        if (driver instanceof RemoteWebDriver) {148            RemoteWebDriver remote = (RemoteWebDriver) driver;149            if (remote.getSessionId() != null) {150                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());151            }152            if (remote.getCapabilities() != null) {153                ex.addInfo("Capabilities", remote.getCapabilities().toString());154            }155        }156        throw ex;157    }158    protected RuntimeException timeoutExceptionNOScreen(String message, Throwable lastException) {159        TimeoutException ex = new TimeoutException(message, lastException);160        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());161        if (driver instanceof RemoteWebDriver) {162            RemoteWebDriver remote = (RemoteWebDriver) driver;163            if (remote.getSessionId() != null) {164                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());165            }166            if (remote.getCapabilities() != null) {167                ex.addInfo("Capabilities", remote.getCapabilities().toString());168            }169        }170        throw ex;171    }172}...Source:AbstractDelegatingWebDriver.java  
...64	public WebElement findElement(By by) {65		try {66			return new DelegatingWebElement(delegate.findElement(by));67		} catch (NoSuchElementException nsee) {68			nsee.addInfo("Find clause", by.toString());69			throw nsee;70		}71	}72	/**73	 * @see org.openqa.selenium.WebDriver#findElements(org.openqa.selenium.By)74	 */75	public List<WebElement> findElements(By by) {76		try {77			List<WebElement> results = new ArrayList<WebElement>();78			for (WebElement webElement : delegate.findElements(by)) {79				results.add(new DelegatingWebElement(webElement));80			}81			return results;82		} catch (NoSuchElementException nsee) {83			nsee.addInfo("Find clause", by.toString());84			throw nsee;85		}86	}87	/**88	 * @see org.openqa.selenium.WebDriver#get(java.lang.String)89	 */90	public void get(String url) {91		delegate.get(url);92	}93	/**94	 * @see org.openqa.selenium.WebDriver#getCurrentUrl()95	 */96	public String getCurrentUrl() {97		return delegate.getCurrentUrl();98	}99	/**100	 * @see org.openqa.selenium.WebDriver#getPageSource()101	 */102	public String getPageSource() {103		return delegate.getPageSource();104	}105	/**106	 * @see org.openqa.selenium.WebDriver#getTitle()107	 */108	public String getTitle() {109		return delegate.getTitle();110	}111	/**112	 * @see org.openqa.selenium.WebDriver#getWindowHandle()113	 */114	public String getWindowHandle() {115		return delegate.getWindowHandle();116	}117	/**118	 * @see org.openqa.selenium.WebDriver#getWindowHandles()119	 */120	public Set<String> getWindowHandles() {121		return delegate.getWindowHandles();122	}123	/**124	 * @see org.openqa.selenium.WebDriver#manage()125	 */126	public Options manage() {127		return delegate.manage();128	}129	/**130	 * @see org.openqa.selenium.WebDriver#navigate()131	 */132	public Navigation navigate() {133		return delegate.navigate();134	}135	/**136	 * @see org.openqa.selenium.WebDriver#switchTo()137	 */138	public TargetLocator switchTo() {139		return delegate.switchTo();140	}141	/**142	 * @see org.openqa.selenium.WebDriver#quit()143	 */144	public void quit() {145		delegate.quit();146	}147	/**148	 * @see org.openqa.selenium.JavascriptExecutor#executeAsyncScript(java.lang.String,149	 *      java.lang.Object[])150	 */151	public Object executeAsyncScript(String script, Object... args) throws WebDriverException {152		try {153			return ((JavascriptExecutor) delegate).executeAsyncScript(script, args);154		} catch (ClassCastException cce) {155			throw new WebDriverException("Delegate implementation `" + delegate.getClass() + "` does not support this feature");156		}157	}158	/**159	 * @see org.openqa.selenium.JavascriptExecutor#executeScript(java.lang.String,160	 *      java.lang.Object[])161	 */162	public Object executeScript(String script, Object... args) throws WebDriverException {163		try {164			return ((JavascriptExecutor) delegate).executeScript(script, args);165		} catch (ClassCastException cce) {166			throw new WebDriverException("Delegate implementation `" + delegate.getClass() + "` does not support this feature");167		}168	}169	/**170	 * @see org.openqa.selenium.TakesScreenshot#getScreenshotAs(org.openqa.selenium.OutputType)171	 */172	public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {173		try {174			return ((TakesScreenshot) delegate).getScreenshotAs(target);175		} catch (ClassCastException cce) {176			throw new WebDriverException("Delegate implementation `" + delegate.getClass() + "` does not support this feature");177		}178	}179	protected class DelegatingWebElement implements WebElement {180		181		protected WebElement delegate;182		public DelegatingWebElement(WebElement delegate) {183			this.delegate = delegate;184		}185		186		@Override187		public void click() {188			delegate.click();189		}190		@Override191		public void submit() {192			delegate.submit();193		}194		@Override195		public void sendKeys(CharSequence... keysToSend) {196			delegate.sendKeys(keysToSend);197		}198		@Override199		public void clear() {200			delegate.clear();201		}202		@Override203		public String getTagName() {204			return delegate.getTagName();205		}206		@Override207		public String getAttribute(String name) {208			return delegate.getAttribute(name);209		}210		@Override211		public boolean isSelected() {212			return delegate.isSelected();213		}214		@Override215		public boolean isEnabled() {216			return delegate.isEnabled();217		}218		@Override219		public String getText() {220			return delegate.getText();221		}222		@Override223		public List<WebElement> findElements(By by) {224			try {225				return delegate.findElements(by);226			} catch (NoSuchElementException nsee) {227				nsee.addInfo("Find clause", by.toString());228				throw nsee;229			}230		}231		@Override232		public WebElement findElement(By by) {233			try {234				return delegate.findElement(by);235			} catch (NoSuchElementException nsee) {236				nsee.addInfo("Find clause", by.toString());237				throw nsee;238			}239		}240		@Override241		public boolean isDisplayed() {242			return delegate.isDisplayed();243		}244		@Override245		public Point getLocation() {246			return delegate.getLocation();247		}248		@Override249		public Dimension getSize() {250			return delegate.getSize();...Source:WebDriverWait.java  
...70  @Override71  protected RuntimeException timeoutException(String message, Throwable lastException) {72    WebDriver exceptionDriver = driver;73    TimeoutException ex = new TimeoutException(message, lastException);74    ex.addInfo(WebDriverException.DRIVER_INFO, exceptionDriver.getClass().getName());75    while (exceptionDriver instanceof WrapsDriver) {76      exceptionDriver = ((WrapsDriver) exceptionDriver).getWrappedDriver();77    }78    if (exceptionDriver instanceof RemoteWebDriver) {79      RemoteWebDriver remote = (RemoteWebDriver) exceptionDriver;80      if (remote.getSessionId() != null) {81        ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());82      }83      if (remote.getCapabilities() != null) {84        ex.addInfo("Capabilities", remote.getCapabilities().toString());85      }86    }87    throw ex;88  }89}Source:WebDriverWaitEx.java  
...46    @Override47    protected RuntimeException timeoutException(String message, Throwable lastException) {48        WebDriver exceptionDriver = driver;49        TimeoutException ex = new TimeoutException(message, lastException);50        ex.addInfo(WebDriverException.DRIVER_INFO, exceptionDriver.getClass().getName());51        while (exceptionDriver instanceof WrapsDriver) {52            exceptionDriver = ((WrapsDriver) exceptionDriver).getWrappedDriver();53        }54        if (exceptionDriver instanceof RemoteWebDriver) {55            RemoteWebDriver remote = (RemoteWebDriver) exceptionDriver;56            if (remote.getSessionId() != null) {57                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());58            }59            if (remote.getCapabilities() != null) {60                ex.addInfo("Capabilities", remote.getCapabilities().toString());61            }62        }63        throw ex;64    }65}...Source:AndroidDriverWait.java  
...4445    @Override46    protected RuntimeException timeoutException(String message, Throwable lastException) {47        TimeoutException ex = new TimeoutException(message, lastException);48        ex.addInfo(WebDriverException.DRIVER_INFO, driver.getClass().getName());49        50        if (driver instanceof RemoteWebDriver) {        	51            RemoteWebDriver remote = (RemoteWebDriver) driver;           52            if (remote.getSessionId() != null) {53                ex.addInfo(WebDriverException.SESSION_ID, remote.getSessionId().toString());54            }55            if (remote.getCapabilities() != null) {56                ex.addInfo("Capabilities", remote.getCapabilities().toString());57            }58        }59        throw ex;60    }61    62}
...addInfo
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.WebDriverException;3import org.openqa.selenium.chrome.ChromeDriver;4public class AddInfoToException {5    public static void main(String[] args) {6        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");7        WebDriver driver = new ChromeDriver();8        try {9        } catch (WebDriverException e) {10            e.addInfo("Selenium version", "3.4.0");11            e.addInfo("Browser", "Chrome");12            e.addInfo("Browser version", "56.0.2924.87");13            e.addInfo("OS", "Mac OS X");14            e.addInfo("OS version", "10.12.3");15            throw e;16        } finally {17            driver.quit();18        }19    }20}21  (unknown error: DevToolsActivePort file doesn't exist)22  (The process started from chrome location /Applications/Google Chrome.app/Contents/MacOS/Google Chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)23  (Driver info: chromedriver=2.29.461591 (0b0e038b6c9b8c1b6f7f0e4d6b7c2f2e2d7c3d3b),platform=Mac OS X 10.12.3 x86_64) (WARNING: The server did not provide any stacktrace information)addInfo
Using AI Code Generation
1import org.openqa.selenium.WebDriverException;2import org.openqa.selenium.By;3import org.openqa.selenium.WebElement;4import org.openqa.selenium.firefox.FirefoxDriver;5import org.openqa.selenium.firefox.FirefoxProfile;6import org.openqa.selenium.support.ui.Select;7import org.openqa.selenium.support.ui.WebDriverWait;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.ExpectedCondition;10import org.openqa.selenium.NoSuchElementException;11import org.openqa.selenium.TimeoutException;12import org.openqa.selenium.StaleElementReferenceException;13import java.util.List;14import java.util.ArrayList;15import java.util.Iterator;16import java.util.concurrent.TimeUnit;17import java.util.concurrent.Callable;18import java.util.concurrent.FutureTask;19import java.util.concurrent.Future;20import java.util.concurrent.Executors;21import java.util.concurrent.ExecutorService;22import java.util.concurrent.ExecutionException;23import java.util.concurrent.TimeoutException;24import java.util.concurrent.atomic.AtomicReference;25import java.util.concurrent.atomic.AtomicBoolean;26import java.lang.reflect.Field;27import java.lang.reflect.Method;28import java.lang.reflect.InvocationTargetException;29import java.lang.reflect.Modifier;30import java.lang.reflect.Constructor;31import java.lang.reflect.Array;32import java.lang.reflect.InvocationTargetException;33import java.lang.reflect.InvocationHandler;34import java.lang.reflect.Proxy;35import java.lang.reflect.UndeclaredThrowableException;36import java.lang.reflect.Type;37import java.lang.reflect.ParameterizedType;38import java.lang.reflect.GenericArrayType;39import java.lang.reflect.TypeVariable;40import java.lang.reflect.GenericDeclaration;41import java.lang.reflect.WildcardType;42import java.lang.reflect.Constructor;43import java.lang.reflect.Field;44import java.lang.reflect.Method;45import java.lang.reflect.Modifier;46import java.io.File;47import java.io.IOException;48import java.io.FileNotFoundException;49import java.io.BufferedReader;50import java.io.FileReader;51import java.io.PrintWriter;52import java.io.FileOutputStream;53import java.io.OutputStreamWriter;54import java.io.BufferedWriter;55import java.io.ByteArrayOutputStream;56import java.io.ByteArrayInputStream;57import java.io.ObjectOutputStream;58import java.io.ObjectInputStream;59import java.io.ObjectStreamClass;60import java.io.Serializable;61import java.io.Externalizable;62import java.io.ObjectStreamField;63import java.io.ObjectOutputStream;64import java.io.ObjectInputStream;65import java.io.ObjectStreamField;66import java.io.ObjectStreamClass;67import java.io.Serializable;68import java.io.Externalizable;69import java.io.IOException;70import java.io.File;71import java.io.IOException;72import java.io.FileNotFoundException;73import java.io.BufferedReader;74import javaaddInfo
Using AI Code Generation
1import org.openqa.selenium.WebDriverException;2public class AddInfoToException {3    public static void main(String[] args) {4        WebDriverException exception = new WebDriverException("This is a WebDriverException");5        exception.addInfo("This is the information we want to add to the exception");6        System.out.println(exception);7    }8}LambdaTest’s Selenium 4 tutorial is covering every aspects of Selenium 4 testing with examples and best practices. Here you will learn basics, such as how to upgrade from Selenium 3 to Selenium 4, to some advanced concepts, such as Relative locators and Selenium Grid 4 for Distributed testing. Also will learn new features of Selenium 4, such as capturing screenshots of specific elements, opening a new tab or window on the browser, and new protocol adoptions.
Upgrading From Selenium 3 To Selenium 4?: In this chapter, learn in detail how to update Selenium 3 to Selenium 4 for Java binding. Also, learn how to upgrade while using different build tools such as Maven or Gradle and get comprehensive guidance for upgrading Selenium.
What’s New In Selenium 4 & What’s Being Deprecated? : Get all information about new implementations in Selenium 4, such as W3S protocol adaption, Optimized Selenium Grid, and Enhanced Selenium IDE. Also, learn what is deprecated for Selenium 4, such as DesiredCapabilites and FindsBy methods, etc.
Selenium 4 With Python: Selenium supports all major languages, such as Python, C#, Ruby, and JavaScript. In this chapter, learn how to install Selenium 4 for Python and the features of Python in Selenium 4, such as Relative locators, Browser manipulation, and Chrom DevTool protocol.
Selenium 4 Is Now W3C Compliant: JSON Wireframe protocol is retiring from Selenium 4, and they are adopting W3C protocol to learn in detail about the advantages and impact of these changes.
How To Use Selenium 4 Relative Locator? : Selenium 4 came with new features such as Relative Locators that allow constructing locators with reference and easily located constructors nearby. Get to know its different use cases with examples.
Selenium Grid 4 Tutorial For Distributed Testing: Selenium Grid 4 allows you to perform tests over different browsers, OS, and device combinations. It also enables parallel execution browser testing, reads up on various features of Selenium Grid 4 and how to download it, and runs a test on Selenium Grid 4 with best practices.
Selenium Video Tutorials: Binge on video tutorials on Selenium by industry experts to get step-by-step direction from automating basic to complex test scenarios with Selenium.
LambdaTest also provides certification for Selenium testing to accelerate your career in Selenium automation testing.
Get 100 minutes of automation test minutes FREE!!
