Could not start a new session. Response code 500. Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line

When I push the code into Git Jenkins gets triggered where all the maven plugins are installed successfully. Still, while starting the browser using java-selenium code, Jenkins throws an error like “org.openqa.selenium.SessionNotCreatedException:
Could not start a new session. Response code 500. Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line”

Note: The same code was executed successfully on my local machine without any error.

The Error which I got in the console:
org.openqa.selenium.SessionNotCreatedException:
Could not start a new session. Response code 500. Message: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOptions.binary’ capability provided, and no binary flag set on the command line
Host info: host: ‘ip-10-1-1-111’, ip: ‘10.1.1.111’
Build info: version: ‘4.7.2’, revision: ‘4d4020c3b7’
System info: os.name: ‘Linux’, os.arch: ‘amd64’, os.version: ‘5.19.0-1025-aws’, java.version: ‘11.0.20.1’
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Command: [null, newSession {capabilities=[Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {args: [-headless]}}], desiredCapabilities=Capabilities {acceptInsecureCerts: true, browserName: firefox, moz:debuggerAddress: true, moz:firefoxOptions: {args: [-headless]}}}]
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:148)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:106)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:535)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:228)
at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:156)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:130)
at org.openqa.selenium.firefox.FirefoxDriver.(FirefoxDriver.java:111)
at org.basepackage.VisionBaseClass.launch(VisionBaseClass.java:146)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.internal.TestInvoker.runConfigMethods(TestInvoker.java:700)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:527)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1541)
at org.testng.TestRunner.privateRun(TestRunner.java:794)
at org.testng.TestRunner.run(TestRunner.java:596)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:308)
at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:71)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:113)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)

java.awt.HeadlessException:

No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.desktop/java.awt.Desktop.getDesktop(Desktop.java:301)
at org.basepackage.VisionBaseClass.generateExtentReport(VisionBaseClass.java:101)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:62)
at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:385)
at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:321)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:344)
at org.testng.SuiteRunner.run(SuiteRunner.java:276)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
at org.testng.TestNG.runSuites(TestNG.java:1063)
at org.testng.TestNG.run(TestNG.java:1031)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:308)
at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:71)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:113)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:385)
at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:162)
at org.apache.maven.surefire.booter.ForkedBooter.run(ForkedBooter.java:507)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:495)

[INFO]
[INFO] Results:

Jenkins setup:
Jenkins: 2.387.3
OS: Linux - 5.19.0-1025-aws
Java: 11.0.20.1 - Ubuntu (OpenJDK 64-Bit Server VM)

ant:487.vd79d090d4ea_e
antisamy-markup-formatter:159.v25b_c67cd35fb_
apache-httpcomponents-client-4-api:4.5.14-150.v7a_b_9d17134a_5
bootstrap5-api:5.3.0-1
bouncycastle-api:2.28
branch-api:2.1092.vda_3c2a_a_f0c11
build-timeout:1.30
caffeine-api:3.1.8-133.v17b_1ff2e0599
checks-api:2.0.0
cloudbees-folder:6.815.v0dd5a_cb_40e0e
commons-lang3-api:3.12.0-36.vd97de6465d5b_
commons-text-api:1.10.0-36.vc008c8fcda_7b_
credentials:1254.vb_96f366e7b_a_d
credentials-binding:604.vb_64480b_c56ca_
display-url-api:2.3.7
durable-task:507.v050055d0cb_dd
echarts-api:5.4.0-4
email-ext:2.97
font-awesome-api:6.3.0-2
git:5.0.2
git-client:4.2.0
github:1.37.1
github-api:1.314-431.v78d72a_3fe4c3
github-branch-source:1703.vd5a_2b_29c6cdc
gradle:2.7
htmlpublisher:1.32
instance-identity:142.v04572ca_5b_265
ionicons-api:56.v1b_1c8c49374e
jackson2-api:2.15.1-344.v6eb_55303dc3e
jakarta-activation-api:2.0.1-3
jakarta-mail-api:2.0.1-3
javadoc:243.vb_b_503b_b_45537
javax-activation-api:1.2.0-6
javax-mail-api:1.6.2-9
jaxb:2.3.8-1
jjwt-api:0.11.5-77.v646c772fddb_0
jquery3-api:3.7.0-1
jsch:0.2.8-65.v052c39de79b_2
junit:1217.v4297208a_a_b_ce
ldap:682.v7b_544c9d1512
mailer:463.vedf8358e006b_
matrix-auth:3.1.7
matrix-project:789.v57a_725b_63c79
maven-plugin:3.23
mina-sshd-api-common:2.10.0-69.v28e3e36d18eb_
mina-sshd-api-core:2.10.0-69.v28e3e36d18eb_
okhttp-api:4.10.0-132.v7a_7b_91cef39c
pam-auth:1.10
pipeline-build-step:491.v1fec530da_858
pipeline-github-lib:42.v0739460cda_c4
pipeline-graph-analysis:202.va_d268e64deb_3
pipeline-groovy-lib:656.va_a_ceeb_6ffb_f7
pipeline-input-step:468.va_5db_051498a_4
pipeline-milestone-step:111.v449306f708b_7
pipeline-model-api:2.2131.vb_9788088fdb_5
pipeline-model-definition:2.2131.vb_9788088fdb_5
pipeline-model-extensions:2.2131.vb_9788088fdb_5
pipeline-rest-api:2.32
pipeline-stage-step:305.ve96d0205c1c6
pipeline-stage-tags-metadata:2.2131.vb_9788088fdb_5
pipeline-stage-view:2.32
plain-credentials:143.v1b_df8b_d3b_e48
plugin-util-api:3.2.1
resource-disposer:0.22
scm-api:672.v64378a_b_20c60
script-security:1275.v23895f409fb_d
slack:664.vc9a_90f8b_c24a_
snakeyaml-api:1.33-95.va_b_a_e3e47b_fa_4
ssh-credentials:305.v8f4381501156
ssh-slaves:2.877.v365f5eb_a_b_eec
sshd:3.303.vefc7119b_ec23
structs:324.va_f5d6774f3a_d
timestamper:1.25
token-macro:359.vb_cde11682e0c
trilead-api:2.84.v72119de229b_7
variant:59.vf075fe829ccb
workflow-aggregator:596.v8c21c963d92d
workflow-api:1213.v646def1087f9
workflow-basic-steps:1017.vb_45b_302f0cea_
workflow-cps:3659.v582dc37621d8
workflow-durable-task-step:1246.v5524618ea_097
workflow-job:1295.v395eb_7400005
workflow-multibranch:746.v05814d19c001
workflow-scm-step:408.v7d5b_135a_b_d49
workflow-step-api:639.v6eca_cd8c04a_a_
workflow-support:839.v35e2736cfd5c
ws-cleanup:0.45

Hello @Jeyasankar and welcome to this community. :wave:

The error message you’re encountering seems to indicate that Jenkins is unable to locate the Firefox binary when attempting to run Selenium tests in a headless mode. :thinking:
This issue typically occurs in headless environments where there is no display server available, and/or Selenium is unable to find the required browser binary.

Here are some steps you could maybe take to try and resolve this issue:

  1. Ensure Firefox is Installed: Make sure that Firefox is installed on the Jenkins machine. Selenium WebDriver requires the browser to be installed locally.
  2. Specify Firefox Binary Location: You can specify the location of the Firefox binary in your Selenium WebDriver configuration. Add the following lines to your Selenium script:
System.setProperty("webdriver.firefox.bin", "/path/to/firefox");
  1. Check PATH Environment Variable: Ensure that the directory containing the Firefox binary is included in the system’s PATH environment variable. This should allow Selenium to find the binary without specifying the exact path.
  2. Headless Mode Compatibility: Firefox may have specific requirements for running in headless mode. Ensure that you have a compatible version of Firefox installed. Some older versions of Firefox may not fully support headless mode.
  3. Use Xvfb (X Virtual Framebuffer): If you’re running Jenkins on a Linux server without a graphical desktop environment, you can use Xvfb to create a virtual display. This should allow Selenium to run in headless mode with Firefox. You’ll need to install Xvfb on your server and configure Jenkins to use it.
  4. Jenkins Agent Configuration: If you’re using Jenkins agents for your builds (and I hope you are using agents), make sure that the agent where the job is running has Firefox and all necessary dependencies installed.
  5. Update Selenium and WebDriver Versions: Ensure that you are using compatible versions of Selenium WebDriver and the WebDriver for Firefox (geckodriver). Updating to the latest versions can sometimes resolve compatibility issues.
  6. Jenkins User Permissions: Check the user permissions under which Jenkins is running. Ensure that the user has permission to access Firefox and execute it in a headless environment.

My $0.2.

Thanks @poddingue I have written java-selenium code for Windows and how can I execute the same code by using Jenkins which is running based on Ubuntu So how do install webdriver or any other configuration that needs to be set up Kindly Explain in detail step by step.