Can somebody tell me how to use the Chrome driver in Selenium for Linux platform?
I have my chrome driver location at username/home/chromedriver.
My code is:
System.setProperty("webdriver.chrome.driver", "/home/username/ChromeDriver/chromedriver");
driver = new ChromeDriver();
driver.get("facebook.com");
The error I am getting is:
org.openqa.selenium.WebDriverException: Unable to either launch or
connect to Chrome. Please check that ChromeDriver is up-to-date.
Using Chrome binary at: /opt/google/chrome/google-chrome
(WARNING: The server did not provide any stacktrace information)
From [the official documentation](https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver:
Requirements
The ChromeDriver controls the browser using Chrome's automation proxy
framework.
The server expects you to have Chrome installed in the default
location for each system:
OS Expected Location of Chrome
-------------------------------------
Linux /usr/bin/google-chrome
Mac /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome
Windows XP %HOMEPATH%\Local Settings\Application Data\Google\Chrome\Application\chrome.exe
Windows Vista C:\Users\%USERNAME%\AppData\Local\Google\Chrome\Application\chrome.exe
For Linux systems, the ChromeDriver expects /usr/bin/google-chrome to be a symlink to the actual Chrome binary. See also the section on overriding the Chrome binary location.
Getting Started
To get set up, first
download the
appropriate prebuilt server. Make sure the server can be located on
your PATH or specify its location via the webdriver.chrome.driver
system property. Finally, all you need to do is create a new
ChromeDriver instance:
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
Therefore, download the version of chromedriver you need, unzip it somewhere on your PATH (or specify the path to it via a system property), then run the driver.
We have installed Successfully
sudo apt-get install unzip
wget -N http://chromedriver.storage.googleapis.com/2.10/chromedriver_linux64.zip -P ~/Downloads
unzip ~/Downloads/chromedriver_linux64.zip -d ~/Downloads
chmod +x ~/Downloads/chromedriver
sudo mv -f ~/Downloads/chromedriver /usr/local/share/chromedriver
Change the directory to /usr/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/local/bin/chromedriver
sudo ln -s /usr/local/share/chromedriver /usr/bin/chromedriver
Now run the script and add the following in the environment file.
Capybara.register_driver :chrome do |app| client = Selenium::WebDriver::Remote::Http::Default.new Capybara::Selenium::Driver.new(app, :browser => :chrome, :http_client => client) end
Capybara.javascript_driver = :chrome
Note : Change the chrome driver version according to your operating system type like 32 bit or 64 bit.
Here is a complete script for Linux 18.04 to install Google Chrome and the chrome driver. It should automatically adjust to collect the correct chrome driver for the browser.
#!/usr/bin/env bash
# install the latest version of Chrome and the Chrome Driver
apt-get update && apt-get install -y libnss3-dev
version=$(curl http://chromedriver.storage.googleapis.com/LATEST_RELEASE)
wget -N http://chromedriver.storage.googleapis.com/${version}/chromedriver_linux64.zip
unzip chromedriver_linux64.zip -d /usr/local/bin
chmod +x /usr/local/bin/chromedriver
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
dpkg -i google-chrome-stable_current_amd64.deb; apt-get -fy install
For me worked with these commands:
Unziped the file -> unzip -q chromedriver_linux64.zip
Force the copy to the directory 'usr/bin' -> sudo mv -f chromedriver /usr/bin
The selenium code was something like that.
System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://mvnrepository.com");
driver.close();
You can see small example from this example
For linux, i downlaod chrome driver and keep as system path variable(or put in exist path folder). And from code i use following ways (add property and initiate with path of chrome driver)
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File("/usr/local/bin/chromedriver"))
.usingAnyFreePort()
.build();
try {
service.start();
} catch (IOException e) {
e.printStackTrace();
}
return new RemoteWebDriver(service.getUrl(), DesiredCapabilities.chrome());
Related
I am unable to get a nodejs script loading Chrome to load a local file.
It works on 18.04 but not 22.04.
Has there been some significant change that would affect local file loading syntax, or is there something wrong in my code?
const { Builder } = require('selenium-webdriver')
async function start() {
const chrome = require('selenium-webdriver/chrome')
const options = new chrome.Options()
options.addArguments('--disable-dev-shm-usage')
options.addArguments('--no-sandbox')
const driver = new Builder()
.forBrowser('chrome')
.setChromeOptions(options)
.build()
await driver.get('file://' + __dirname + '/myfile.html')
await driver.sleep(10000)
const text = await driver.executeScript('return document.documentElement.innerText')
console.log(text)
driver.quit()
}
start()
The result is:
Your file couldn’t be accessed
It may have been moved, edited or deleted.
ERR_FILE_NOT_FOUND
I can confirm that myfile.html is definitely present.
Using console.log to show the file argument value shows it is the same for both older and newer Ubuntu.
Changing the driver.get argument to a website, e.g. https://www.google.com/ correctly shows the webpage content in the output.
The local file code fails as above using:
Ubuntu 22.04.1 LTS
Node v16.15.0
chromium-browser Chromium 108.0.5359.71 snap
It works fine on:
Ubuntu 18.04.6 LTS
Node v16.15.0
Chromium 107.0.5304.87 Built on Ubuntu , running on Ubuntu 18.04
This seems to be because the default Ubuntu 22.04 Chrome package is a snap package that limits local file access to /home/ .
Switching to the .deb distribution from Google solves the issue. The Chromedriver also needs to match.
# Chrome browser - .deb package, not the standard OS Snap which limits access to only /home/
# See: https://askubuntu.com/questions/1184357/why-cant-chromium-suddenly-access-any-partition-except-for-home
# See: https://www.ubuntuupdates.org/ppa/google_chrome?dist=stable
wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list'
apt-get update
apt-get install google-chrome-stable
# Chromedriver, matching version number
# See: https://skolo.online/documents/webscrapping/#pre-requisites
# See step 3 at: https://tecadmin.net/setup-selenium-chromedriver-on-ubuntu/
# See: https://sites.google.com/chromium.org/driver/
#!# NB This will become out of sync if apt-get updates google-chrome-stable
google-chrome --version
wget https://chromedriver.storage.googleapis.com/108.0.5359.71/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
rm chromedriver_linux64.zip
I am looking for a way to update the PATH in Jenkins for running Selenium tests with PyTest.
I need to run the latest version of chromedriver but due to an infrastructure deficiency (our base image is running Debian and the latest available version is 73 and I need to be running at least 83).
There is already a version of chromedriver installed on the image at /usr/bin and I need to be able to point to a different version
The jenkins chromedriver plugin appears that it will just use the latest available version for debian, which doesn't help me at all.
Until I have time to address the systemic issue, I'd like to just install chromedriver and update PATH - because Selenium requires chromedriver on the PATH
It seemed, for ease of use, that https://pypi.org/project/chromedriver-binary/ was a good solution - it installs just fine and the shell script chromedriver-path echoes the location, so I could just update path as the documentation shows: PATH=$PATH:chromedriver-path
This doesn't seem to jive in Jenkins - PATH is not updated
stages {
stage('build'){
steps {
withCredentials([...]) {
sh """
alias python=python3.8
python -m venv --system-site-packages venv # only for jenkins
python -u setup.py
. venv/bin/activate
which chromedriver #/usr/bin/chromedriver
chromedriver-path #path/to/python/lib/python3.8/site-packages/chromedriver_binary
export PATH=$PATH:`chromedriver-path`
which chromedriver #/usr/bin/chromedriver
"""
sh "python -m pytest"
}
}
}
}
I Have looked at the withEnv() option and environment{} step but I'm not sure how to access that binary and update PATH once chromedriver-binary has been set - because it appears that environment{} would not have access to shell scripts that are installed in the individual steps.
Any tips would be greatly appreciated
The issue may actually be in Jenkinsfile declaration.
Try using sh with single quotes '''. Also binaries are searched in directories defined in PATH from left to right, so to override system PATH, you must put your directory on the beginning, not on the end.
If I alter your code snippet:
stages {
stage('build'){
steps {
withCredentials([...]) {
sh '''
alias python=python3.8
python -m venv --system-site-packages venv # only for jenkins
python -u setup.py
. venv/bin/activate
which chromedriver #/usr/bin/chromedriver
chromedriver-path #path/to/python/lib/python3.8/site-packages/chromedriver_binary
export PATH=$(chromedriver-path):$PATH
echo $PATH # just to check the output, your path should be on the beginning
which chromedriver # this should now find the proper chromedriver
'''
sh "python -m pytest"
}
}
}
}
Environment: Centos7, Chromedriver2.24, chrome-browser-stable version 2.53, selenium version 2.53.1 and xvfb
When running tests parallel with 8 concurrent threads giving session not found exception..
Chrome-setup:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList(
"--disable-logging", "--silent", "--log-level 3"));
capabilities.setCapability("chrome.logfile", "NUL");
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
System.setProperty("webdriver.chrome.driver",
"servers/chromedriver-local");
driver = new ChromeDriver(capabilities);
Any help much appreciated!!
This is what fixed my issue:
Simpy mount -v /dev/shm:/dev/shm
Or, longer, create a big shm
Started in privileged mode: docker run --privileged
Fix small /dev/shm size
docker exec $id sudo umount /dev/shm
docker exec $id sudo mount -t tmpfs -o rw,nosuid,nodev,noexec,relatime,size=512M tmpfs /dev/shm
However it would be nice to avoid privileged mode.
(Ref:github.com/elgalu/docker-selenium/issues/20)
Using Laravel 5+ with Vagrant.
I have selenium running using:
java -jar vendor/se/selenium-server-standalone/bin/selenium-server-standalone.jar -Dwebdriver.firefox.bin="/usr/bin/firefox"
using a headless display:
sudo Xvfb :10 -ac
However when i run codeception:
./vendor/bin/codecept run selenium --steps
I get the following error:
[Facebook\WebDriver\Exception\UnknownServerException] Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: GDK_BACKEND does not match available displays
I am confused with server and ports that i should be using.
Currently i access site through http://localhost:8000
however that is outside vagrant.
I observe $url = 'http://localhost:4444/wd/hub', inside api\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php
As the error outputs :
127.0.0.1 on port 7055.
Actually you should download latest geckodriver from here and set property Dwebdriver.gecko.driver with downloaded geckodriver path from your system when you run the selenium-server-standalone.jar as below :
java -jar vendor/se/selenium-server-standalone/bin/selenium-server-standalone.jar -Dwebdriver.firefox.bin="/usr/bin/firefox" -Dwebdriver.gecko.driver = "path/to/downloaded geckodriver
Note : Just like the other drivers available to Selenium from other browser vendors, Mozilla has released an executable geckodriver that will run alongside with the latest firefox browser. For more information you should follow this link.
Now you need to set capability with marionette to true during initialisation of RemoteWebDriver inside api\vendor\facebook\webdriver\lib\Remote\RemoteWebDriver.php as :
$capabilities->setCapability('marionette', true);
Full example code :
$host = 'http://localhost:4444/wd/hub';
$capabilities = DesiredCapabilities::firefox();
$capabilities->setCapability('marionette', true);
$driver = RemoteWebDriver::create($host, $capabilities, 5000);
Switched to chrome web driver, was less complicated, more compatible and works fine for me.
Download chrome webdriver or composer require
Setup environment
nohup sudo Xvfb :10 -ac &
export DISPLAY=:10
java -jar vendor/se/selenium-server-standalone/bin/selenium-server-standalone.jar -Dwebdriver.chrome.bin="/usr/bin/google-chrome" -Dwebdriver.chrome.driver="vendor/bin/chromedriver"
I'm using Selenium RC in a Ubuntu system.
I want to automate the tests, and I need to start Selenium-server.jar on startup of the machine.
I created seleniumServer.conf in /ect/init/ with:
start on startup
start on runlevel 3
respawn
exec xvfb-run java -jar /home/condde/selenium-server-1.0.3/selenium-server.jar -port 4444
When I reboot the machine, it works fine, the process is running.
But when I execute a test, the result is:
PHPUnit_Framework_Exception: Could not connect to the Selenium RC server.
Any ideas?
Thanks!
I have the same problem, my process can not connect the selenium server sometimes. After dig into debug log and selenium source code, I found that's because java's SecureRandom hangs if /dev/random hangs when selenium try generate random number. So I replace /dev/random with /dev/urandom, then selenium server works fine:
sudo mv /dev/random /dev/random.real
sudo ln -s /dev/urandom /dev/random
Or you can modify $JAVA_HOME/jre/lib/security/java.security file and changing the property:
securerandom.source=file:/dev/random
to:
securerandom.source=file:/dev/urandom
Maybe it works, but not for me.
Use -debug to start Selenium with debug log to see if any error.
java -jar selenium-server.jar -debug > /var/log/selenium-server.log 2>&1
I did this on ubuntu 14 using npm.
First, install the selenium-standalone via npm.
sudo npm install selenium-standalone -g
sudo selenium-standalone install
Then create a symbolic link in /etc/init.d, and configure it to run.
sudo ln -s /usr/local/bin/selenium-standalone /etc/init.d/
sudo update-rc.d selenium-standalone defaults
Another very simple and good solution is to install selenium via docker. I have used the chrome image and it's easy as:
sudo docker run -d -p 4444:4444 selenium/standalone-chrome
The -d option makes is a daemon that will be restarted every time you start your computer. The -p option forwards the webdriver port (4444) from the docker instance to the host.
Well, it's not phantomjs, but I like chrome better anyway. There is also a firefox image! Checkout https://github.com/SeleniumHQ/docker-selenium for more info.
I would start the selenium server process with -log parameter to get info from the process first and all and see if it actually get any kind of connections, errors etc..
A few ideas to troubleshoot:
Do you get any response if you enter http://localhost:4444
It should render a 403 error by the Jetty engine.
If this does not work I would try with your actual IP:4444, that might indicate problem with localhost variable, proxy settings etc..
Could the firewall settings be blocking the the 4444 port? Maybe the Selenium Server process is not allowed to start the browser.