selenium 启动chromedriver时出现NoSuchSessionException异常

pinkon5k  于 2022-12-13  发布在  其他
关注(0)|答案(3)|浏览(170)

我正在尝试创建一个简单的测试,只需打开页面并验证其标题。我认为这不能再简单了,但是...我的测试在当前打开的Chrome浏览器中打开了一个新标签,并且在driver.get(myUrl);上失败,因为org.openqa.selenium.NoSuchSessionException
我试过升级和降级chromedriver和升级降级selenium,但到目前为止没有成功。目前我正在使用selenium-java 3.3.1,chromedriver 2.28和chrome 57.0.2987.98。任何帮助都很感谢。
额外的问题,上次我使用chromedriver它打开了一个新的浏览器窗口,有什么办法回到那个吗?我更喜欢我的测试在一个新的窗口,而不是一个新的标签。

public class MyExampleTest {
    private String baseUrl;
    private WebDriver driver;

    @Before
    public void openBrowser() {
        baseUrl = "http://myurl/";
        System.setProperty("webdriver.chrome.driver", "C:\\Projects\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(baseUrl);
    }

    @Test
    public void pageTitleAfterSearchShouldBeThere() throws IOException {
        assertEquals("The page title should be this at the start of the test.", "my title", driver.getTitle());
    }
}

堆栈跟踪:

org.openqa.selenium.NoSuchSessionException: no such session
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 13 milliseconds
Build info: version: '3.3.1', revision: '5234b325d5', time: '2017-03-10 09:10:29 +0000'
System info: host: 'XXXXXXXXXXXXXXX', ip: 'xxx.xxx.xxx.xx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_121'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{message=unknown error: Chrome failed to start: crashed
  (Driver info: chromedriver=2.28.455520 (cc17746adff54984afff480136733114c6b3704b),platform=Windows NT 6.1.7601 SP1 x86_64), platform=ANY}]
Session ID: 359ce80c1b82e53fffae055451228404
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:216)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:168)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:638)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:325)
    at ec_selenium.CapExampleTest.openBrowser(CapExampleTest.java:58)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
pxiryf3j

pxiryf3j1#

这是因为不匹配的chromedriver版本和兼容的webdriver版本。要么改变它的任何一个或采取两个最新的。这对我工作!

c3frrgcw

c3frrgcw2#

您需要检查chrome驱动程序下载页面https://chromedriver.chromium.org/downloads
上面写着兼容性......另外,看看这里有一个兼容的驱动程序n浏览器https://stackoverflow.com/a/55266105/6305768的列表

7fhtutme

7fhtutme3#

你可能搞砸了imports。试试下面的代码,对我来说很好。

import static org.junit.Assert.assertEquals;

import java.io.IOException;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyExampleTest {
    private String baseUrl;
    private WebDriver driver;

    @Before
    public void openBrowser() {
        baseUrl = "http://facebook.com/";
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();
    }

    @Test
    public void pageTitleAfterSearchShouldBeThere() throws IOException {
        System.out.println(driver.getTitle());
        assertEquals("The page title should be this at the start of the test.", "Facebook - Log In or Sign Up", driver.getTitle());
    }

    @After
    public void closeBrowser(){
        try{
        driver.quit();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
}

这将打开一个new window每次不是一个新的标签在已经打开的chrome浏览器。

相关问题