在Windows 10 Enterpise 64位(Microsoft Edge版本:25.10586.672.0)和Microsoft WebDriver-版本10240的本地计算机上运行selenium时出现错误。我的Selenium版本为:3.6.0
public class SeleniumTest { private WebDriver driver; @BeforeClass public void getWebDriver() { try { System.setProperty("webdriver.edge.driver", "myapp/driver/MicrosoftWebDriver.exe"); DesiredCapabilities capabilities = DesiredCapabilities.edge(); capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true); capabilities.setCapability(CapabilityType.PAGE_LOAD_STRATEGY, "eager"); capabilities.setPlatform(Platform.WIN10); capabilities.setBrowserName(BrowserType.EDGE); capabilities.setVersion(""); driver = new EdgeDriver(capabilities); } catch (Exception e) { e.printStackTrace(); } driver.get(Constant.URL); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS); } @AfterClass public void quitDriver() throws InterruptedException { Thread.sleep(3000); driver.quit(); } @Test () public void aTest() { } @Test () public void bTest() { } }
当我运行代码时,打开边缘浏览器并出现错误:
org.openqa.selenium.NoSuchSessionException: null (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 873 milliseconds Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T15:28:36.4Z' System info: host: 'computername', ip: 'myip', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_111' Driver info: driver.version: EdgeDriver
您可能会考虑研究发行说明中提到的内容:
更新.NET绑定以不发送不正确的W3C Firefox功能以前,RemoteWebDriver会在请求新的远程会话时使用“ desiredCapabilities”和“ capabilities”属性发送相同的功能字典。在明确要求使用旧版Firefox驱动程序的语言绑定的情况下,功能字典将包含对兼容W3C的远程服务器无效的属性。为了解决该问题,我们将通过设置一个属性来掩盖显式尝试,该属性使.NET RemoteWebDriver在显式请求旧版驱动程序时发送仅旧版兼容的新会话请求。
我看不到您的代码中有任何重大错误,唯一的错误是 NoSuchSessionException 。代替:
NoSuchSessionException
DesiredCapabilities capabilities = DesiredCapabilities.edge();
您应该使用:
DesiredCapabilities cap = new DesiredCapabilities();