Selenium NoSuchSession on linux

2019-08-25 02:59发布

问题:

i tried to run selenium tests created on windows machine. I change driver to linux version. Added it to PATH. But every time i got

org.selenium.NoSUchSessionException

i use latest browser with latest driver

I define driver it like that :

public class AuthTestSteps {
private static WebDriver driver;
private static WebDriverWait wait;
@Given("^blah_blah$")
public void method() throws MalformedURLException{

    driver = new ChromeDriver();
    wait = new WebDriverWait(driver, 30);
    System.setProperty("webdriver.chrome.driver","chromedriver");
}

Solution:

im my case solution was adding driver manager and options to chrome like "--no-sandbox", because it was run from root user.

回答1:

While executing Selenium Tests you need to pass the absolute path of the WebDriver binary first through System.setProperty() line then initialize the Web Browser as follows :

public class AuthTestSteps 
{
    private static WebDriver driver;

    @Given("^blah_blah$")
    public void method() throws MalformedURLException
    {

        System.setProperty("webdriver.chrome.driver","/path/to/chromedriver");
        driver = new ChromeDriver();
    }
}


回答2:

Use absolute path of chrome driver. change your code like this.

public class AuthTestSteps {
private static WebDriver driver;
private static WebDriverWait wait;
@Given("^blah_blah$")
public void method() throws MalformedURLException{

    // assuming that your chrome driver is located inside
    // your project(src/main/resources/browser_driver/chromedriver)
    // take absolute path for chrome driver
    File file = new File("src/main/resources/browser_driver/chromedriver");
    String absolutePath = file.getAbsolutePath();

    driver = new ChromeDriver();
    wait = new WebDriverWait(driver, 30);
    System.setProperty("webdriver.chrome.driver", absolutePath);
}