This tutorial will help you to set up Selenium with ChromeDriver on Ubuntu, and LinuxMint systems. This tutorial also includes an example of a Java program that uses a Selenium standalone server and ChromeDriver and runs a sample test case.
Read This: Setup Selenium with Firefox on Ubuntu
Step 1 – Prerequisites
Execute the following commands to install the required packages on your system. Here Xvfb (X virtual framebuffer) is an in-memory display server for a UNIX-like operating system (e.g., Linux). It implements the X11 display server protocol without any display. This is helpful for CLI applications like CI services.
sudo apt update
sudo apt install -y unzip xvfb libxi6 libgconf-2-4
Also, install Java on your system. Let’s install Oracle Java 8 on your system or use the below command to install OpenJDK.
sudo apt install default-jdk
Step 2 – Install Google Chrome
Now install Latest Google chrome package on your system using the below list commands. Google chrome headless feature opens multipe doors for the automation.
sudo curl -sS -o - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list"
sudo apt -y update
sudo apt -y install google-chrome-stable
Step 3 – Installing ChromeDriver
You are also required to set up ChromeDriver on your system. ChromeDriver is a standalone server that implements WebDriver’s wire protocol for Chromium. The WebDriver is an open-source tool for the automated testing of web apps across multiple browsers.
Find out the Google chrome version installed on your system.
google-chrome --version
OutputGoogle Chrome 94.0.4606.71
Next, visit the Chromedriver download page and download the matching version of chromedriver
on your system.
In my case, Google Chrome 94 is running on my system. So download the following file. You must make sure to download the correct version of a file:
wget https://chromedriver.storage.googleapis.com/94.0.4606.61/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
You can find the latest ChromeDriver on its official download page. Now execute below commands to configure ChromeDriver on your system.
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
Step 4 – Download Required Jar Files
The Selenium Server is required to run Remote Selenium WebDrivers. You need to download the Selenium standalone server jar file using the below commands or visit here to find the latest version of Jar file.
wget https://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar
mv selenium-server-standalone-3.141.59.jar selenium-server-standalone.jar
Also, download the testng-6.8.7.jar file to your system.
wget http://www.java2s.com/Code/JarDownload/testng/testng-6.8.7.jar.zip
unzip testng-6.8.7.jar.zip
Step 5 – Start Chrome via Selenium Server
Your server setup is ready. Start Chrome via a standalone selenium server using the Xvfb utility.
Run Chrome via Selenium Server
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar
Use -debug
option at end of the command to start the server in debug mode.
You can also Start Headless ChromeDriver by typing the below command on the terminal.
chromedriver --url-base=/wd/hub
Your Selenium server is now running with Chrome. Use this server to run your test cases written in Selenium using the Google Chrome web browser. The next step is an optional step and doesn’t depend on Step 5.
Step 6 – Sample Java Program (Optional)
This is an optional step. It describes running a single test case using a Selenium standalone server and ChromeDriver. Let’s create a Java program using the Selenium server and Chrome Driver. This Java program will open a specified website URL and check if a defined string is present on the webpage or not.
Create a Java program by editing a file in a text editor.
vim TecAdminSeleniumTest.java
Add the below content to the file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.IOException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.Test; public class TecAdminSeleniumTest { public static void main(String[] args) throws IOException, InterruptedException { System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver"); ChromeOptions chromeOptions = new ChromeOptions(); chromeOptions.addArguments("--headless"); chromeOptions.addArguments("--no-sandbox"); WebDriver driver = new ChromeDriver(chromeOptions); driver.get("https://google.com"); Thread.sleep(1000); if (driver.getPageSource().contains("I'm Feeling Lucky")) { System.out.println("Pass"); } else { System.out.println("Fail"); } driver.quit(); } } |
You can change the URL “https://google.com” with any other URL of your choice, Then also change the search string like “I’m Feeling Lucky” used in the above Java program. Save your java program and execute it. First, you need to set the Java CLASSPATH environment variable including the selenium-server-standalone.jar and testng-6.8.7.jar. Then compile the java program and run it.
export CLASSPATH=".:selenium-server-standalone.jar:testng-6.8.7.jar"
javac TecAdminSeleniumTest.java
java TecAdminSeleniumTest
You will see the results below. If the defined search string is found, You will get the message “Pass” and if the string is not found on the webpage, you will get the “Fail” message on the screen.
42 Comments
Hello, thank you for the tutorial but the chrome browser didnāt start for me, it seems like it I started in headless mode
when I run this command :
āxvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jarā
I got this :
11:52:18.535 INFO [GridLauncherV3.parse] ā Selenium server version: 3.141.59, revision: e82be7d358
11:52:18.615 INFO [GridLauncherV3.lambda$buildLaunchers$3] ā Launching a standalone Selenium Server on port 4444
2022-03-01 11:52:18.659:INFO::main: Logging initialized @324ms to org.seleniumhq.jetty9.util.log.StdErrLog
11:52:18.900 INFO [WebDriverServlet.] ā Initialising WebDriverServlet
11:52:18.984 INFO [SeleniumServer.boot] ā Selenium Server is up and running on port 4444
No errors no crash but chrome didnāt start.
cool. Worked like a charm
Thanks so much for making this guide on setting up selenium, chrome and chromedriver on linux.
with this command:
sudo curl -sS -o – https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
I was getting this error:
E: This command can only be used by root.
curl: (23) Failed writing body (1313 != 1396)
This was the fix:
sudo curl -sS -o – https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add
Adding ‘sudo’ before apt-key add
Hello , thank you for the tutorial but the chrome browser didn’t start for me , it seems like it i started in headless mode,
when i run this command :
“xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar”
I got this :
11:52:18.535 INFO [GridLauncherV3.parse] – Selenium server version: 3.141.59, revision: e82be7d358
11:52:18.615 INFO [GridLauncherV3.lambda$buildLaunchers$3] – Launching a standalone Selenium Server on port 4444
2022-03-01 11:52:18.659:INFO::main: Logging initialized @324ms to org.seleniumhq.jetty9.util.log.StdErrLog
11:52:18.900 INFO [WebDriverServlet.] – Initialising WebDriverServlet
11:52:18.984 INFO [SeleniumServer.boot] – Selenium Server is up and running on port 4444
No errors no crash but chrome didn’t start
Thanks for the excellent tutorial, I finished the Selenium project I was working on. I am using WSL and it used to launch my desktop browser (Brave) when running the AWS CLI SSO login, for example, but now the AWS CLI SSO login launches a Chrome window which doesn’t have any of my data saved in it like Brave does. Do you have any idea how I can reset so that WSL launches Brave again like before rather than Chrome? I deleted the chromedriver but that made no difference. Any advice is much appreciated š
Turns out it was as simple as setting an environment variable named BROWSER with the value being the path to my Brave exe š
This command is incorrect:
sudo echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google-chrome.list
It uses sudo which means it’s designed for non-root accounts, but the way it’s written the redirection happens before sudo or echo are run, which means it’s run as the current user, resulting in a “Permission denied” error. That command would only work if it’s run by root, but then you wouldn’t need sudo.
For it to work for non-root accounts, the entire command has to be wrapped in a shell that is invoked with sudo:
sudo bash -c "echo 'deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main' >> /etc/apt/sources.list.d/google-chrome.list"
Hello Rahul,
Its not working yet
I run this cmd ——– xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar
I got this ——Error: Unable to access jarfile selenium-server-standalone.jar
Regards
V
–
Use Version of selenium-server -standalone jar files.
For eg: xvfb-run java -Dwebdriver.chrome.dr iver=/usr/bin/chromedriver -jar selenium-server-standalone-3.141.59.jar
its working š thnx man
I don’t see browser running when I execute below command
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone3.13.0.jar
Users your inputs please..
Browser will open, once you run the test case. In the given example, remove “chromeOptions.addArguments(“–headless”);” from script to launch a web browser.
Worked with few changes on Ubuntu 20.
sudo curl -sS -o – https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add
=>
curl -sS -o – https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo apt-key add
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar
=>
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.13.0.jar
Thanks for your post.
Thanks for Post
very nice tutorial . Thanks
Input
xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.141.59.jar
Output
Command ‘xvfb-run’ not found, but can be installed with:
apt install xvfb
Input
apt install xvfd
Output
E: Unable to find xvfd package
Thanks š
Very sorry. I’ve found my mistake – d
thank you so much! I use this tutorial for python3 with selenium google driver
hi,
I am executing my automation scripts on oneops cloud application. To execute these automation scripts we need to have chrome driver. In the oneops application we have Linux64 bit OS is available. For this OS, i downloaded the relevant chrome driver and placed in a specific path. In my automation script I gave this specific path. I developed a jar file with all my automation scripts using looper\jenkins. Now I am executing this jar file in the putty(linux) machine. However an error message chromedriver is not present in the specified path is displaying though the driver is present in the specified path.
This chrome driver for Linux is of file type (not .exe ) and placed this driver in a path.
I developed these automation scripts using selenium testng.
Please let me know why this error message of chrome driver not present is displaying.
Also if any one knows how to install chrome driver in oneops cloud application for Linux OS, please do let me know.
Thanks
Regards
K.Radhakrishna Reddy
Hello! Thanks for the great article. However I am quite stuck with the error below.
PHP Fatal error: Uncaught PHPUnit_Extensions_Selenium2TestCase_WebDriverException: Expected browser binary location, but unable to find binary in default location, no ‘moz:firefoxOpti
ons.binary’ capability provided, and no binary flag set on the command line
I got both geckodriver and chromedriver on usr/bin/* .
Thank you
Thank you, your guide comes up first in google search results! congratulations! however, after following your steps, command “xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone.jar” gives me an error: “Error: Unable to access jarfile selenium-server-standalone.jar”
I have managed(I think so) to run it using line from comments below: “xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.13.0.jar” but now I get new problems.
When I try : “java TecAdminSeleniumTest” I get the error messages: “TecAdminSeleniumTest.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver”
any updates?
Hi, Thanks for the great article.
Couple of corrections needed:
1. Switch to root user from Step1
2. Step 5: xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.13.0.jar (version number missed out)
3. Step 6: export CLASSPATH=”.:selenium-server-standalone-3.13.0.jar:testng-6.8.7.jar” (Selenium Server version number missed out)
Could you please make this changes to your article?
Thank you!
Thanks for this shared setup.
Thank you for very helpful post!!!!
thanks rahul..
HI ,
I am trying to start chromedriver from linux server. Please help me on how to run command prompt. Getting exception like below
———————————–
/usr/bin$ ./chromedriver
Starting ChromeDriver 2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706) on port 9515
Only local connections are allowed.
————————————————-
Hi SAI,
As per the command output, it looks chromedriver is started properly on port 9515.
Getting error :
(The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=2.41.578700 (2f1ed5f9343c13f73144538f15c00b370eda6706),platform=Linux 4.4.0-1072-aws x86_64) (WARNING: The server did not provide any stacktrace information)
Or just use `sudo apt-get install chromium-browser chromium-chromedriver`
A lot thanks
So So Great, thank you.
Iām glad it was useful to me. Thanks for your work. Ill be in touch
Good work Rahul. Very well explained.
Thx buddy for the great support
Thank you!
One note: At step 5 , to start the server, I had to include the version… xvfb-run java -Dwebdriver.chrome.driver=/usr/bin/chromedriver -jar selenium-server-standalone-3.13.0.jar
Just wanna say this was really helpful. Although, for whatever reason, I needed to use ‘sudo -i’ before step 2, and sudo as a prefix for some commands.
Thank you. This helped me out immensely.
Excelent post! Working at first shot.
Thank you very much for sharing your knowledge.
PD: Last chromedrive version is 2.41
Please update for others => wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
Why you do not show users comment on your website. Thanks, by the way, I will try this selenium installation guide.