如何通过Selenium RemoteWebDriver在服务器上无需ChromeDriver.exe启动谷歌浏览器?
我希望在服务器上不使用ChromeDriver.exe启动谷歌浏览器,尝试用RemoteWebDriver但出现404错误。
My Code
public class DEMO { public static void main(String[] args) throws MalformedURLException { // create a Chrome Web Driver URL local = new URL("http://localhost:8080"); WebDriver driver = new RemoteWebDriver(local, DesiredCapabilities.chrome()); // open the browser and go to open google.com driver.get("https://www.google.com"); driver.manage().window().maximize(); } }
Error Message
Exception in thread "main" org.openqa.selenium.UnsupportedCommandException:
Apache Tomcat/7.0.47 - Error report HTTP Status 404 - /sessiontype Status report
message /session
description The requested resource is not available.
Apache Tomcat/7.0.47
Command duration or timeout: 333 milliseconds
Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:01:03'
Hey there, let's break down what's causing this 404 error and fix it step by step:
Root Cause
Your code is pointing the RemoteWebDriver to Apache Tomcat (port 8080) instead of a running Selenium Server/Grid Hub. Tomcat has no idea what the /session endpoint is (that's a Selenium-specific path for creating browser sessions)—so it throws a 404.
Step-by-Step Fix
Grab the matching Selenium Server JAR
You're using Selenium version 2.53.0, so make sure to download the exact matchingselenium-server-standalone-2.53.0.jar(version mismatch will cause weird issues later).Start the Selenium Server
Open a command prompt/terminal and run this command (replace the file path with where you saved the JAR):java -jar selenium-server-standalone-2.53.0.jarBy default, this starts the server on port
4444, and the correct endpoint for session creation ishttp://localhost:4444/wd/hub.If ChromeDriver isn't in your system PATH, you need to specify its location when starting the server:
java -Dwebdriver.chrome.driver="/path/to/your/chromedriver" -jar selenium-server-standalone-2.53.0.jarUpdate your Java code
Change the URL to point to the Selenium Server's correct endpoint, not Tomcat:public class DEMO { public static void main(String[] args) throws MalformedURLException { // Point to Selenium Server's hub endpoint (not Tomcat!) URL seleniumHub = new URL("http://localhost:4444/wd/hub"); WebDriver driver = new RemoteWebDriver(seleniumHub, DesiredCapabilities.chrome()); // open the browser and go to google.com driver.get("https://www.google.com"); driver.manage().window().maximize(); } }
Why This Works
- The Selenium Server acts as a middleman between your code and the browser, handling communication via ChromeDriver behind the scenes.
- The
/wd/hubpath is the standard endpoint for Selenium's JSON Wire Protocol, which yourRemoteWebDriveruses to send commands like creating a session or navigating to a URL.
内容的提问来源于stack exchange,提问作者Suyash

