尝试通过 FirefoxProfile 打开网站时出现 "NoSuchSessionException" 错误

Getting error as "NoSuchSessionException" when trying to open an website through FirefoxProfile

我是运行下面的代码,要开一个URL。但是,我收到错误 "NoSuchSessionException"。请建议。

是不是因为我用的是以下版本。

Selenium--> 3.12.0, Firefox Setup 50.0 and 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");

}

}

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

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

您有 2 种方法可以使用现有的 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");
        }
    }
    

注意:确保您已经创建了 Firefox 配置文件 作为 Abhi_Selenium 在触发 Test.

之前

更新

由于您仍然看到异常 没有这样的会话,请执行以下 upgradation/cleanup 步骤:

  • JDK升级到最近的水平JDK 8u181
  • Selenium 升级到当前级别 Version 3.13.0
  • GeckoDriver 升级到 GeckoDriver v0.20.1 级别。
  • 确保 GeckoDriver 存在于指定位置。
  • 确保 GeckoDriver 具有非根用户的可执行权限。
  • Firefox 版本升级到 Firefox v61.0.1 级别。
  • 清理你的项目工作区通过你的IDE重建你的项目只需要依赖。
  • (WindowsOS only) 使用 CCleaner 工具擦除执行 OS 之前和之后的所有琐事 测试套件.
  • (LinuxOS only) Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint 在你的 Test Suite 执行前后。
  • 如果您的基础 Web 客户端 版本太旧,则通过 Revo Uninstaller 卸载它并安装最新的 GA 和发布版本的 Web 客户端.
  • 系统重启
  • 以非 root 用户身份执行 Test
  • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例.

乍一看,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

}