简体   繁体   中英

Java run Chromedriver with selenium on Ubuntu server

I have a Java web servlet that I want to host on an Ubuntu 18.04 server running Tomcat 8. The servlet uses selenium and chromedriver during execution. The servlet was running fine on my local machine which is running Tomcat 8 on Windows 10. I tried to edit the code to run on my server as below:

static WebDriver browser;
static AutomatedPromethee automatedPromethee;
static ChromeOptions options = new ChromeOptions();

options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");
options.setBinary(new File("/var/lib/tomcat8/webapps/chromedriver"));

//capabilities.setCapability(ChromeOptions.CAPABILITY, options);
browser = new ChromeDriver(options);

I managed to successfully import the selenium libraries and also copied the chromedriver file to a folder on my server. However, the webapp does not open a new browser instance as expected as per the last line of code above. How can I solve this issue?

After a lot of trouble, I finally managed to solve this issue. Here's a chronology of the things I did, may be some weren't necessary, but who knows?

*Install xvfb and dependencies

apt-get install xvfb libxi6 libgconf-2-4

*Install google chrome browser and make sure its at /usr/bin/google-chrome (Symlink is also ok).

*Copy chromedriver to /usr/local/bin/

*Make sure that chromedriver has +x (executable) permission

Then run the following commands on Ubuntu:

Xvfb -ac :99 -screen 0 1280x1024x16 &
export DISPLAY=:99

My final servlet code was as follows:

static WebDriver browser;
static ChromeOptions options = new ChromeOptions();

System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");

options.addArguments("--headless");
options.addArguments("--no-sandbox");
options.addArguments("start-maximized");
options.addArguments("disable-infobars");
options.addArguments("--disable-extensions");

browser = new ChromeDriver(options);

This worked for me. I hope it helps someone else too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM