我的selenium代码未运行。不断向我抛出以下错误:
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Build info: version: 'unknown', revision: 'unknown', time: 'unknown' System info: host: 'SLAP129', ip: '192.168.4.218', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74' Driver info: driver.version: FirefoxDriver at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:658) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:250) at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:236) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:137) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:191) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:108) at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:104) at PractiseSession1.OpenBrowser(PractiseSession1.java:35) at PractiseSession1.main(PractiseSession1.java:16) Caused by: java.lang.NullPointerException at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:192) at org.openqa.selenium.firefox.XpiDriverService.start(XpiDriverService.java:94) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637) ... 8 more
FIREFOX版本:53.0.3(32位)SELENIUM版本:selenium-java-3.4.0我正在使用Eclipse Luna,并且我的机器是Windows 7(64位)。我已经阅读了所有帮助查询,但找不到解决方案。这是我的Java代码:
import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class PractiseSession1 { public static void main(String[] args) { // TODO Auto-generated method stub WebDriver driver = null; String URL="http://www.google.com"; //System.out.println("Application title is ============="); PractiseSession1 ade= new PractiseSession1(); ade.OpenBrowser(driver); ade.GetPage(URL, driver); ade.quitbrowser(driver); } private void quitbrowser(WebDriver driver) { driver.quit(); } private void GetPage(String URL, WebDriver driver) { driver.get(URL); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } private void OpenBrowser(WebDriver driver) { System.setProperty("webdriver.firefox.marionette", "<Geckodriver path>geckodriver.exe"); DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette", true); driver= new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } }
您必须在代码中考虑几个因素,如下所示:
PractiseSession1 ade= new PractiseSession1();
ade
OpenBrowser()
GetPage()
quitbrowser()
main()
webdriver.gecko.driver
webdriver.firefox.marionette
System.setProperty
在提及时,System.setProperty您需要提供 geckodriver.exe 的绝对路径,如下所示:
System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe");
一旦提及ImplicitlyWait,它将在程序执行期间保留下来。您可以考虑删除多个提及。
ImplicitlyWait
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
package demo; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class Q44308973_remote_unreachablebrowserexception { public static void main(String[] args) { System.setProperty("webdriver.gecko.driver", "C:\\your_directory\\geckodriver.exe"); DesiredCapabilities dc = DesiredCapabilities.firefox(); dc.setCapability("marionette", true); WebDriver driver = new FirefoxDriver(dc); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("https://google.com"); } }