This exception occurs when Selenium tries to click an element, but the click would instead
be received by a different element. Before Selenium will click an element, it checks if the
element is visible, unobscured by any other elements, and enabled - if the element is obscured,
it will raise this exception.
Likely Cause
UI Elements Overlapping
Elements on the UI are typically placed next to each other, but occasionally elements may overlap.
For example, a navbar always staying at the top of your window as you scroll a page. If that navbar
happens to be covering an element we are trying to click, Selenium might believe it to be visible
and enabled, but when you try to click it will throw this exception. Pop-ups and Modals are also
common offenders here.
Animations
Elements with animations have the potential to cause this exception as well - it is recommended
to wait for animations to cease before attempting to click an element.
Possible Solutions
Use Explicit Waits
Explicit Waits will likely be your best friend
in these instances. A great way is to use ExpectedCondition.ToBeClickable()
with WebDriverWait to wait until the right moment.
Scroll the Element into View
In instances where the element is out of view, but Selenium still registers the element as visible
(e.g. navbars overlapping a section at the top of your screen), you can use the
WebDriver.executeScript() method to execute a javascript function to scroll
(e.g. WebDriver.executeScript('window.scrollBy(0,-250)')) or you can utilize the
Actions class with Actions.moveToElement(element).
隐藏的元素:元素存在于 DOM 中,但由于 CSS、hidden 属性或元素超出可见视口范围而不可见。
可能的解决方案
根据元素类型使用适当的操作(例如,仅对 <input> 字段使用 sendKeys)。
确保定位器唯一标识目标元素,以避免错误匹配。
在与元素交互之前,检查其是否在页面上可见。如果需要,将元素滚动到视图中。
使用显式等待以确保元素在执行操作前可交互。
1.1 - Unable to Locate Driver Error
Troubleshooting missing path to driver executable.
Historically, this is the most common error beginning Selenium users get
when trying to run code for the first time:
The path to the driver executable must
be set by the webdriver.chrome.driver system property;
for more information, see https://chromedriver.chromium.org/.
The latest version can be downloaded from https://chromedriver.chromium.org/downloads
The executable chromedriver needs to be available in the path.
The file geckodriver does not exist. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases"
Unable to locate the chromedriver executable;
Likely cause
Through WebDriver, Selenium supports all major browsers.
In order to drive the requested browser, Selenium needs to
send commands to it via an executable driver.
This error means the necessary driver could not be
found by any of the means Selenium attempts to use.
Possible solutions
There are several ways to ensure Selenium gets the driver it needs.
Use the latest version of Selenium
As of Selenium 4.6, Selenium downloads the correct driver for you.
You shouldn’t need to do anything. If you are using the latest version
of Selenium and you are getting an error,
please turn on logging
and file a bug report with that information.
If you want to read more information about how Selenium manages driver downloads for you,
you can read about the Selenium Manager.
This is a flexible option to change location of drivers without having to update your code,
and will work on multiple machines without requiring that each machine put the
drivers in the same place.
You can either place the drivers in a directory that is already listed in PATH,
or you can place them in a directory and add it to PATH.
To see what directories are already on PATH, open a Terminal and execute:
echo$PATH
If the location to your driver is not already in a directory listed,
you can add a new directory to PATH:
You can test if it has been added correctly by checking the version of the driver:
chromedriver --version
To see what directories are already on PATH, open a Command Prompt and execute:
echo %PATH%
If the location to your driver is not already in a directory listed,
you can add a new directory to PATH:
setx PATH "%PATH%;C:\WebDriver\bin"
You can test if it has been added correctly by checking the version of the driver:
chromedriver.exe --version
Specify the location of the driver
If you cannot upgrade to the latest version of Selenium, you
do not want Selenium to download drivers for you, and you can’t figure
out the environment variables, you can specify the location of the driver in the Service object.
Specifying the location in the code itself has the advantage of not needing
to figure out Environment Variables on your system, but has the drawback of
making the code less flexible.
Driver management libraries
Before Selenium managed drivers itself, other projects were created to
do so for you.
If you can’t use Selenium Manager because you are using
an older version of Selenium (please upgrade),
or need an advanced feature not yet implemented by Selenium Manager,
you might try one of these tools to keep your drivers automatically updated:
importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.util.Arrays;importjava.util.logging.FileHandler;importjava.util.logging.Handler;importjava.util.logging.Level;importjava.util.logging.Logger;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.manager.SeleniumManager;importorg.openqa.selenium.remote.RemoteWebDriver;publicclassLoggingTest{@AfterEachpublicvoidloggingOff(){Loggerlogger=Logger.getLogger("");logger.setLevel(Level.INFO);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.INFO);});}@Testpublicvoidlogging()throwsIOException{Loggerlogger=Logger.getLogger("");logger.setLevel(Level.FINE);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.FINE);});Handlerhandler=newFileHandler("selenium.xml");logger.addHandler(handler);Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);LoggerlocalLogger=Logger.getLogger(this.getClass().getName());localLogger.warning("this is a warning");localLogger.info("this is useful information");localLogger.fine("this is detailed debug information");byte[]bytes=Files.readAllBytes(Paths.get("selenium.xml"));StringfileContent=newString(bytes);Assertions.assertTrue(fileContent.contains("this is a warning"));Assertions.assertTrue(fileContent.contains("this is useful information"));Assertions.assertTrue(fileContent.contains("this is detailed debug information"));}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L31" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Python logs are typically created per module. You can match all submodules by referencing the top
level module. So to work with all loggers in selenium module, you can do this:
deftest_logging(log_path):logger=logging.getLogger('selenium')logger.setLevel(logging.DEBUG)handler=logging.FileHandler(log_path)logger.addHandler(handler)logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)logger.info("this is useful information")logger.warning("this is a warning")logger.debug("this is detailed debug information")withopen(log_path,'r')asfp:assertlen(fp.readlines())==3
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/troubleshooting/test_logging.py#L5" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
You must also create and add a log handler (StreamHandler, FileHandler, etc).
.NET logger is managed with a static class, so all access to logging is managed simply by referencing Log from the OpenQA.Selenium.Internal.Logging namespace.
If you want to see as much debugging as possible in all the classes,
you can turn on debugging globally in Ruby by setting $DEBUG = true.
For more fine-tuned control, Ruby Selenium created its own Logger class to wrap the default Logger class.
This implementation provides some interesting additional features.
Obtain the logger directly from the #loggerclass method on the Selenium::WebDriver module:
require'spec_helper'RSpec.describe'Logging'dolet(:file_name){Tempfile.new('logging').path}after{FileUtils.rm_f(file_name)}it'logs things'dologger=Selenium::WebDriver.loggerlogger.level=:debuglogger.output=file_namelogger.ignore(:jwp_caps,:logger_info)logger.allow(%i[selenium_managerexample_id])logger.warn('this is a warning',id::example_id)logger.info('this is useful information',id::example_id)logger.debug('this is detailed debug information',id::example_id)expect(File.readlines(file_name).grep(/[:example_id]/).size).toeq3endend
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/troubleshooting/logging_spec.rb#L11" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.util.Arrays;importjava.util.logging.FileHandler;importjava.util.logging.Handler;importjava.util.logging.Level;importjava.util.logging.Logger;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.manager.SeleniumManager;importorg.openqa.selenium.remote.RemoteWebDriver;publicclassLoggingTest{@AfterEachpublicvoidloggingOff(){Loggerlogger=Logger.getLogger("");logger.setLevel(Level.INFO);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.INFO);});}@Testpublicvoidlogging()throwsIOException{Loggerlogger=Logger.getLogger("");logger.setLevel(Level.FINE);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.FINE);});Handlerhandler=newFileHandler("selenium.xml");logger.addHandler(handler);Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);LoggerlocalLogger=Logger.getLogger(this.getClass().getName());localLogger.warning("this is a warning");localLogger.info("this is useful information");localLogger.fine("this is detailed debug information");byte[]bytes=Files.readAllBytes(Paths.get("selenium.xml"));StringfileContent=newString(bytes);Assertions.assertTrue(fileContent.contains("this is a warning"));Assertions.assertTrue(fileContent.contains("this is useful information"));Assertions.assertTrue(fileContent.contains("this is detailed debug information"));}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L32-L35" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Python has 6 logger levels: CRITICAL, ERROR, WARNING, INFO, DEBUG, and NOTSET.
The default is WARNING
deftest_logging(log_path):logger=logging.getLogger('selenium')logger.setLevel(logging.DEBUG)handler=logging.FileHandler(log_path)logger.addHandler(handler)logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)logger.info("this is useful information")logger.warning("this is a warning")logger.debug("this is detailed debug information")withopen(log_path,'r')asfp:assertlen(fp.readlines())==3
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/troubleshooting/test_logging.py#L7" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Things get complicated when you use PyTest, though. By default, PyTest hides logging unless the test
fails. You need to set 3 things to get PyTest to display logs on passing tests.
To always output logs with PyTest you need to run with additional arguments.
First, -s to prevent PyTest from capturing the console.
Second, -p no:logging, which allows you to override the default PyTest logging settings so logs can
be displayed regardless of errors.
So you need to set these flags in your IDE, or run PyTest on command line like:
pytest -s -p no:logging
Finally, since you turned off logging in the arguments above, you now need to add configuration to
turn it back on:
logging.basicConfig(level=logging.WARN)
.NET has 6 logger levels: Error, Warn, Info, Debug, Trace and None. The default level is Info.
usingOpenQA.Selenium;usingOpenQA.Selenium.Internal.Logging;usingOpenQA.Selenium.Remote;usingSystem;usingSystem.IO;namespaceSeleniumDocs.Troubleshooting{[TestClass]publicclassLoggingTest{privateconststringfilePath="Selenium.log";[TestMethod]publicvoidLogging(){Log.SetLevel(LogEventLevel.Trace);Log.Handlers.Add(newFileLogHandler(filePath));Log.SetLevel(typeof(RemoteWebDriver),LogEventLevel.Debug);Log.SetLevel(typeof(SeleniumManager),LogEventLevel.Info);Warn("this is a warning");Info("this is useful information");Debug("this is detailed debug information");using(varfileStream=File.Open(filePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){using(varstreamReader=newStreamReader(fileStream)){varfileLogContent=streamReader.ReadToEnd();StringAssert.Contains(fileLogContent,"this is a warning");StringAssert.Contains(fileLogContent,"this is useful information");StringAssert.Contains(fileLogContent,"this is detailed debug information");}}}[TestCleanup]publicvoidTestCleanup(){// reset log to defaultLog.SetLevel(LogEventLevel.Info).Handlers.Clear().Handlers.Add(newConsoleLogHandler());}// logging is only for internal usage// hacking it via reflectionprivatevoidDebug(stringmessage){LogMessage("Debug",message);}privatevoidWarn(stringmessage){LogMessage("Warn",message);}privatevoidInfo(stringmessage){LogMessage("Info",message);}privatevoidLogMessage(stringmethodName,stringmessage){vargetLoggerMethod=typeof(Log).GetMethod("GetLogger",System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.NonPublic,newType[]{typeof(Type)});varlogger=getLoggerMethod.Invoke(null,newobject[]{typeof(LoggingTest)});varemitMethod=logger.GetType().GetMethod(methodName);emitMethod.Invoke(logger,newobject[]{message});}}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/dotnet/SeleniumDocs/Troubleshooting/LoggingTest.cs#L18" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Ruby logger has 5 logger levels: :debug, :info, :warn, :error, :fatal.
As of Selenium v4.9.1, The default is :info.
require'spec_helper'RSpec.describe'Logging'dolet(:file_name){Tempfile.new('logging').path}after{FileUtils.rm_f(file_name)}it'logs things'dologger=Selenium::WebDriver.loggerlogger.level=:debuglogger.output=file_namelogger.ignore(:jwp_caps,:logger_info)logger.allow(%i[selenium_managerexample_id])logger.warn('this is a warning',id::example_id)logger.info('this is useful information',id::example_id)logger.debug('this is detailed debug information',id::example_id)expect(File.readlines(file_name).grep(/[:example_id]/).size).toeq3endend
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/troubleshooting/logging_spec.rb#L13" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
JavaScript has 9 logger levels: OFF, SEVERE, WARNING, INFO, DEBUG, FINE, FINER, FINEST, ALL.
The default is OFF.
To change the level of the logger:
logger.setLevel(logging.Level.INFO)
Content Help
Note: This section needs additional and/or updated content
importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.util.Arrays;importjava.util.logging.FileHandler;importjava.util.logging.Handler;importjava.util.logging.Level;importjava.util.logging.Logger;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.manager.SeleniumManager;importorg.openqa.selenium.remote.RemoteWebDriver;publicclassLoggingTest{@AfterEachpublicvoidloggingOff(){Loggerlogger=Logger.getLogger("");logger.setLevel(Level.INFO);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.INFO);});}@Testpublicvoidlogging()throwsIOException{Loggerlogger=Logger.getLogger("");logger.setLevel(Level.FINE);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.FINE);});Handlerhandler=newFileHandler("selenium.xml");logger.addHandler(handler);Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);LoggerlocalLogger=Logger.getLogger(this.getClass().getName());localLogger.warning("this is a warning");localLogger.info("this is useful information");localLogger.fine("this is detailed debug information");byte[]bytes=Files.readAllBytes(Paths.get("selenium.xml"));StringfileContent=newString(bytes);Assertions.assertTrue(fileContent.contains("this is a warning"));Assertions.assertTrue(fileContent.contains("this is useful information"));Assertions.assertTrue(fileContent.contains("this is detailed debug information"));}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L37-L38" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
By default all logs are sent to sys.stderr. To direct output somewhere else, you need to add a
handler with either a StreamHandler or a FileHandler:
deftest_logging(log_path):logger=logging.getLogger('selenium')logger.setLevel(logging.DEBUG)handler=logging.FileHandler(log_path)logger.addHandler(handler)logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)logger.info("this is useful information")logger.warning("this is a warning")logger.debug("this is detailed debug information")withopen(log_path,'r')asfp:assertlen(fp.readlines())==3
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/python/tests/troubleshooting/test_logging.py#L9-L10" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
By default all logs are sent to System.Console.Error output. To direct output somewhere else, you need to add a handler with a FileLogHandler:
usingOpenQA.Selenium;usingOpenQA.Selenium.Internal.Logging;usingOpenQA.Selenium.Remote;usingSystem;usingSystem.IO;namespaceSeleniumDocs.Troubleshooting{[TestClass]publicclassLoggingTest{privateconststringfilePath="Selenium.log";[TestMethod]publicvoidLogging(){Log.SetLevel(LogEventLevel.Trace);Log.Handlers.Add(newFileLogHandler(filePath));Log.SetLevel(typeof(RemoteWebDriver),LogEventLevel.Debug);Log.SetLevel(typeof(SeleniumManager),LogEventLevel.Info);Warn("this is a warning");Info("this is useful information");Debug("this is detailed debug information");using(varfileStream=File.Open(filePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){using(varstreamReader=newStreamReader(fileStream)){varfileLogContent=streamReader.ReadToEnd();StringAssert.Contains(fileLogContent,"this is a warning");StringAssert.Contains(fileLogContent,"this is useful information");StringAssert.Contains(fileLogContent,"this is detailed debug information");}}}[TestCleanup]publicvoidTestCleanup(){// reset log to defaultLog.SetLevel(LogEventLevel.Info).Handlers.Clear().Handlers.Add(newConsoleLogHandler());}// logging is only for internal usage// hacking it via reflectionprivatevoidDebug(stringmessage){LogMessage("Debug",message);}privatevoidWarn(stringmessage){LogMessage("Warn",message);}privatevoidInfo(stringmessage){LogMessage("Info",message);}privatevoidLogMessage(stringmethodName,stringmessage){vargetLoggerMethod=typeof(Log).GetMethod("GetLogger",System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.NonPublic,newType[]{typeof(Type)});varlogger=getLoggerMethod.Invoke(null,newobject[]{typeof(LoggingTest)});varemitMethod=logger.GetType().GetMethod(methodName);emitMethod.Invoke(logger,newobject[]{message});}}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/dotnet/SeleniumDocs/Troubleshooting/LoggingTest.cs#L20" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
By default, logs are sent to the console in stdout. To store the logs in a file:
require'spec_helper'RSpec.describe'Logging'dolet(:file_name){Tempfile.new('logging').path}after{FileUtils.rm_f(file_name)}it'logs things'dologger=Selenium::WebDriver.loggerlogger.level=:debuglogger.output=file_namelogger.ignore(:jwp_caps,:logger_info)logger.allow(%i[selenium_managerexample_id])logger.warn('this is a warning',id::example_id)logger.info('this is useful information',id::example_id)logger.debug('this is detailed debug information',id::example_id)expect(File.readlines(file_name).grep(/[:example_id]/).size).toeq3endend
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/troubleshooting/logging_spec.rb#L15" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
JavaScript does not currently support sending output to a file.
To send logs to console output:
logging.installConsoleHandler()
Content Help
Note: This section needs additional and/or updated content
importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;importjava.util.Arrays;importjava.util.logging.FileHandler;importjava.util.logging.Handler;importjava.util.logging.Level;importjava.util.logging.Logger;importorg.junit.jupiter.api.AfterEach;importorg.junit.jupiter.api.Assertions;importorg.junit.jupiter.api.Test;importorg.openqa.selenium.manager.SeleniumManager;importorg.openqa.selenium.remote.RemoteWebDriver;publicclassLoggingTest{@AfterEachpublicvoidloggingOff(){Loggerlogger=Logger.getLogger("");logger.setLevel(Level.INFO);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.INFO);});}@Testpublicvoidlogging()throwsIOException{Loggerlogger=Logger.getLogger("");logger.setLevel(Level.FINE);Arrays.stream(logger.getHandlers()).forEach(handler->{handler.setLevel(Level.FINE);});Handlerhandler=newFileHandler("selenium.xml");logger.addHandler(handler);Logger.getLogger(RemoteWebDriver.class.getName()).setLevel(Level.FINEST);Logger.getLogger(SeleniumManager.class.getName()).setLevel(Level.SEVERE);LoggerlocalLogger=Logger.getLogger(this.getClass().getName());localLogger.warning("this is a warning");localLogger.info("this is useful information");localLogger.fine("this is detailed debug information");byte[]bytes=Files.readAllBytes(Paths.get("selenium.xml"));StringfileContent=newString(bytes);Assertions.assertTrue(fileContent.contains("this is a warning"));Assertions.assertTrue(fileContent.contains("this is useful information"));Assertions.assertTrue(fileContent.contains("this is detailed debug information"));}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/java/src/test/java/dev/selenium/troubleshooting/LoggingTest.java#L40-L41" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Because logging is managed by module, instead of working with just "selenium", you can specify
different levels for different modules:
1114
Show full example
importloggingdeftest_logging(log_path):logger=logging.getLogger('selenium')logger.setLevel(logging.DEBUG)handler=logging.FileHandler(log_path)logger.addHandler(handler)logging.getLogger('selenium.webdriver.remote').setLevel(logging.WARN)logging.getLogger('selenium.webdriver.common').setLevel(logging.DEBUG)logger.info("this is useful information")logger.warning("this is a warning")logger.debug("this is detailed debug information")withopen(log_path,'r')asfp:assertlen(fp.readlines())==3
usingOpenQA.Selenium;usingOpenQA.Selenium.Internal.Logging;usingOpenQA.Selenium.Remote;usingSystem;usingSystem.IO;namespaceSeleniumDocs.Troubleshooting{[TestClass]publicclassLoggingTest{privateconststringfilePath="Selenium.log";[TestMethod]publicvoidLogging(){Log.SetLevel(LogEventLevel.Trace);Log.Handlers.Add(newFileLogHandler(filePath));Log.SetLevel(typeof(RemoteWebDriver),LogEventLevel.Debug);Log.SetLevel(typeof(SeleniumManager),LogEventLevel.Info);Warn("this is a warning");Info("this is useful information");Debug("this is detailed debug information");using(varfileStream=File.Open(filePath,FileMode.Open,FileAccess.Read,FileShare.ReadWrite)){using(varstreamReader=newStreamReader(fileStream)){varfileLogContent=streamReader.ReadToEnd();StringAssert.Contains(fileLogContent,"this is a warning");StringAssert.Contains(fileLogContent,"this is useful information");StringAssert.Contains(fileLogContent,"this is detailed debug information");}}}[TestCleanup]publicvoidTestCleanup(){// reset log to defaultLog.SetLevel(LogEventLevel.Info).Handlers.Clear().Handlers.Add(newConsoleLogHandler());}// logging is only for internal usage// hacking it via reflectionprivatevoidDebug(stringmessage){LogMessage("Debug",message);}privatevoidWarn(stringmessage){LogMessage("Warn",message);}privatevoidInfo(stringmessage){LogMessage("Info",message);}privatevoidLogMessage(stringmethodName,stringmessage){vargetLoggerMethod=typeof(Log).GetMethod("GetLogger",System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.NonPublic,newType[]{typeof(Type)});varlogger=getLoggerMethod.Invoke(null,newobject[]{typeof(LoggingTest)});varemitMethod=logger.GetType().GetMethod(methodName);emitMethod.Invoke(logger,newobject[]{message});}}}
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/dotnet/SeleniumDocs/Troubleshooting/LoggingTest.cs#L22-L23" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Ruby’s logger allows you to opt in (“allow”) or opt out (“ignore”) of log messages based on their IDs.
Everything that Selenium logs includes an ID. You can also turn on or off all deprecation notices by
using :deprecations.
These methods accept one or more symbols or an array of symbols:
require'spec_helper'RSpec.describe'Logging'dolet(:file_name){Tempfile.new('logging').path}after{FileUtils.rm_f(file_name)}it'logs things'dologger=Selenium::WebDriver.loggerlogger.level=:debuglogger.output=file_namelogger.ignore(:jwp_caps,:logger_info)logger.allow(%i[selenium_managerexample_id])logger.warn('this is a warning',id::example_id)logger.info('this is useful information',id::example_id)logger.debug('this is detailed debug information',id::example_id)expect(File.readlines(file_name).grep(/[:example_id]/).size).toeq3endend
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/troubleshooting/logging_spec.rb#L17" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
require'spec_helper'RSpec.describe'Logging'dolet(:file_name){Tempfile.new('logging').path}after{FileUtils.rm_f(file_name)}it'logs things'dologger=Selenium::WebDriver.loggerlogger.level=:debuglogger.output=file_namelogger.ignore(:jwp_caps,:logger_info)logger.allow(%i[selenium_managerexample_id])logger.warn('this is a warning',id::example_id)logger.info('this is useful information',id::example_id)logger.debug('this is detailed debug information',id::example_id)expect(File.readlines(file_name).grep(/[:example_id]/).size).toeq3endend
<div class="text-end pb-2 mt-2">
<a href="https://github.com/SeleniumHQ/seleniumhq.github.io/blob/display_full//examples/ruby/spec/troubleshooting/logging_spec.rb#L18" target="_blank">
<i class="fas fa-external-link-alt pl-2"></i>
<strong>View full example on GitHub</strong>
</a>
</div>
Content Help
Note: This section needs additional and/or updated content
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
# frozen_string_literal: truerequire'spec_helper'RSpec.describe'Chrome'dodescribe'Driver Options'dolet(:chrome_location){driver_finder&&ENV.fetch('CHROME_BIN',nil)}let(:url){'https://www.selenium.dev/selenium/web/'}it'page load strategy normal'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:normaldriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy eager'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:eagerdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'page load strategy none'dooptions=Selenium::WebDriver::Options.chromeoptions.page_load_strategy=:nonedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets remote capabilities',skip:'this is example code that will not execute'dooptions=Selenium::WebDriver::Options.firefoxoptions.platform_name='Windows 10'options.browser_version='latest'cloud_options={}cloud_options[:build]=my_test_buildcloud_options[:name]=my_test_nameoptions.add_option('cloud:options',cloud_options)driver=Selenium::WebDriver.for:remote,capabilities:optionsdriver.get(url)driver.quitendit'accepts untrusted certificates'dooptions=Selenium::WebDriver::Options.chromeoptions.accept_insecure_certs=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets unhandled prompt behavior'dooptions=Selenium::WebDriver::Options.chromeoptions.unhandled_prompt_behavior=:acceptdriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets window rect'dooptions=Selenium::WebDriver::Options.firefoxoptions.set_window_rect=truedriver=Selenium::WebDriver.for:firefox,options:optionsdriver.get(url)driver.quitendit'sets strict file interactability'dooptions=Selenium::WebDriver::Options.chromeoptions.strict_file_interactability=truedriver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the proxy'dooptions=Selenium::WebDriver::Options.chromeoptions.proxy=Selenium::WebDriver::Proxy.new(http:'myproxy.com:8080')driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the implicit timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={implicit:1}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the page load timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={page_load:400_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets the script timeout'dooptions=Selenium::WebDriver::Options.chromeoptions.timeouts={script:40_000}driver=Selenium::WebDriver.for:chrome,options:optionsdriver.get(url)driver.quitendit'sets capabilities in the pre-selenium 4 way',skip:'this is example code that will not execute'docaps=Selenium::WebDriver::Remote::Capabilities.firefoxcaps[:platform]='Windows 10'caps[:version]='92'caps[:build]=my_test_buildcaps[:name]=my_test_namedriver=Selenium::WebDriver.for:remote,url:cloud_url,desired_capabilities:capsdriver.get(url)driver.quitendendend
<dependencies><!-- more dependencies ... --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>3.141.59</version></dependency><!-- more dependencies ... --></dependencies>
After
<dependencies><!-- more dependencies ... --><dependency><groupId>org.seleniumhq.selenium</groupId><artifactId>selenium-java</artifactId><version>4.4.0</version></dependency><!-- more dependencies ... --></dependencies>