尝试通过FirefoxProfile打开网站时收到“ NoSuchSessionException”错误

时间:2018-07-30 04:36:14

标签: java selenium firefox selenium-webdriver geckodriver

我正在运行以下代码,以打开URL。但是,我收到“ NoSuchSessionException”错误。请提示。

是因为我正在使用以下版本。

  

Selenium-> 3.12.0,Firefox Setup 50.0和geckodriver-v0.21.0-win64

import java.util.concurrent.TimeUnit;
   import org.openqa.selenium.WebDriver;
   import org.openqa.selenium.firefox.FirefoxDriver;
   import org.openqa.selenium.firefox.FirefoxOptions;
   import org.openqa.selenium.firefox.FirefoxProfile;
   import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Gmail {

public static void main(String[] args){

System.setProperty("webdriver.gecko.driver", "D:\\Drivers\\geckodriver.exe");

FirefoxOptions options = new FirefoxOptions();  
ProfilesIni allProf = new ProfilesIni();// all profiles
FirefoxProfile prof = allProf.getProfile("Abhi_Selenium");
options.setProfile(prof);

//FirefoxDriver driver = new FirefoxDriver(options);

WebDriver driver = new FirefoxDriver(options);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://gmail.com");

}

}

3 个答案:

答案 0 :(得分:0)

您可以使用 FireFoxProfile 类和 FirefoxOptions 类来设置配置文件。

FirefoxOptions options = new FirefoxOptions(); 
FirefoxProfile firefoxProfile  = new FirefoxProfile(pathToProfile);
options.setProfile(firefoxProfile);

答案 1 :(得分:0)

您可以通过以下两种方式使用现有的 Firefox配置文件访问 Web应用程序

  • 使用DesiredCapabilities()FirefoxOptions()

    public class FirefoxProfile_dc_opt {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            ProfilesIni profile = new ProfilesIni();
            FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
            DesiredCapabilities dc = DesiredCapabilities.firefox();
            dc.setCapability(FirefoxDriver.PROFILE, testprofile);
            FirefoxOptions opt = new FirefoxOptions();
            opt.merge(dc);
            WebDriver driver =  new FirefoxDriver(opt);
            driver.get("https://www.google.com");
        }
    }
    
  • 使用FirefoxOptions()

    public class FirefoxProfile_opt {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            ProfilesIni profile = new ProfilesIni();
            FirefoxProfile testprofile = profile.getProfile("Abhi_Selenium");
            FirefoxOptions opt = new FirefoxOptions();
            opt.setProfile(testprofile);
            WebDriver driver =  new FirefoxDriver(opt);
            driver.get("https://www.google.com");
        }
    }
    

注意:在触发 Test 之前,请确保已将 Firefox配置文件创建为 Abhi_Selenium


更新

由于您仍然看到异常,例如没有这样的会话,请执行以下升级/清理步骤:

  • JDK 升级到最新级别JDK 8u181
  • 升级到当前水平Version 3.13.0
  • GeckoDriver 升级到GeckoDriver v0.20.1级。
  • 确保 GeckoDriver 位于指定位置。
  • 确保 GeckoDriver 具有非root用户的可执行权限。
  • Firefox 版本升级到 Firefox v61.0.1 级别。
  • 通过您的 IDE
  • 清理您的项目工作区重建您的项目,并且仅具有必需的依赖项。
  • (仅适用于 WindowsOS )使用CCleaner工具清除执行 Test Suite 前后的所有操作系统琐事。
  • (仅仅LinuxOS Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint在执行 Test Suite 之前和之后。
  • 如果您的基本 Web客户端版本过旧,请通过Revo Uninstaller进行卸载,并安装最新版本的 Web客户端。 li>
  • 进行系统重启
  • 以非root用户身份执行Test
  • 始终在driver.quit()方法内调用tearDown(){},以优雅地关闭和销毁 WebDriver Web Client 实例。

答案 2 :(得分:0)

乍一看,缺少firefox.exe的路径。有我的设置:

public class foo{
    private static WebDriver driver;

@BeforeClass
    public static void setUpClass() {
        FirefoxOptions options = new FirefoxOptions();
        ProfilesIni allProfiles = new ProfilesIni();         
        FirefoxProfile selenium_profile = allProfiles.getProfile("selenium_profile");
        options.setProfile(selenium_profile);
        options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
        System.setProperty("webdriver.gecko.driver", "C:\\Users\\pburgr\\Desktop\\geckodriver-v0.20.0-win64\\geckodriver.exe");
        driver = new FirefoxDriver(options);
        driver.manage().window().maximize();}

// @Before, @After, @AfterClass and @Test

}