本地启动Selenium正常,远程启动异常的问题求助
Hey Bob, I’ve run into this exact scenario before—having to manually log into a machine to fire up Selenium every time tests need to run is a total pain. Let’s get this sorted so your Jenkins/Codeception setup can run tests without you being physically present.
Recommended Solution: Run Selenium as a System Service (Most Reliable)
The best long-term fix is to configure Selenium Server to start automatically as a system service, so it runs in the background even when no one’s logged into the machine. Here’s how to do it for common OSes:
Linux (Systemd)
- Create a systemd service file (replace paths and username with your actual details):
# /etc/systemd/system/selenium-chrome.service [Unit] Description=Selenium Server with ChromeDriver After=network.target [Service] User=your-login-username ExecStart=/usr/bin/java -Dwebdriver.chrome.driver=/full/path/to/chromedriver238 -jar /full/path/to/selenium-server-3.11.jar Restart=always RestartSec=10 # If your machine uses a desktop environment, add this to ensure Chrome can access the display Environment=DISPLAY=:0 [Install] WantedBy=multi-user.target - Reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable --now selenium-chrome.service
This will start Selenium immediately, and it’ll automatically restart if it crashes or the machine reboots.
Windows
- Use a tool like NSSM (Non-Sucking Service Manager) to wrap the Selenium command into a Windows service:
- Download NSSM, open Command Prompt as admin, navigate to the NSSM folder.
- Run
nssm install SeleniumChrome - In the NSSM GUI:
- Path: Browse to your
java.exe(usually inC:\Program Files\Java\your-jdk-version\bin\java.exe) - Arguments:
-Dwebdriver.chrome.driver=C:\path\to\chromedriver238.exe -jar C:\path\to\selenium-server-3.11.jar - Set a Display name like "Selenium Server with ChromeDriver"
- Path: Browse to your
- Save the service, then start it from Services.msc. You can also set it to start automatically on boot.
Temporary Workaround: Background Processes (For Quick Testing)
If you need a quick fix without setting up a service, you can run Selenium in the background so it stays running after you log out:
Linux/macOS
- Use
nohupto detach the process from your terminal:
The output will be logged tonohup java -Dwebdriver.chrome.driver=chromedriver238 -jar selenium-server-3.11.jar > selenium-output.log 2>&1 &selenium-output.log, and the process will keep running even after you exit the terminal. - Or use
screento create a persistent terminal session:screen -S selenium-session # Run your Selenium start command here # Press Ctrl+A then D to detach from the session # Later, reattach with: screen -r selenium-session
Critical Checks to Ensure Remote Access Works
Even if Selenium is running, double-check these to avoid connection issues:
- Firewall Rules: Make sure the Selenium port (default 4444) is open on the dedicated machine so your Jenkins server can reach it.
- User Permissions: If running as a system service, ensure the service user has access to ChromeDriver, Chrome, and any required directories. Manual runs use your user account, but services often run as root or a dedicated system user—test the command as that user to confirm it works.
- Codeception Configuration: Verify your Jenkins/Codeception setup is pointing to the correct Selenium URL:
http://[selenium-machine-ip]:4444/wd/hub
内容的提问来源于stack exchange,提问作者Bob Arezina

