java - 尝试通过 FirefoxProfile 打开网站时出现“NoSuchSessionException”错误
问题描述
我正在运行以下代码来打开一个 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");
}
}
解决方案
您有 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。
更新
由于您仍然看到没有此类会话的异常,请执行以下升级/清理步骤:
- 将JDK升级到最新级别JDK 8u181。
- 将Selenium升级到当前级别Version 3.13.0。
- 将GeckoDriver升级到GeckoDriver v0.20.1级别。
- 确保GeckoDriver存在于指定位置。
- 确保GeckoDriver对非 root 用户具有可执行权限。
- 将Firefox版本升级到Firefox v61.0.1级别。
- 通过IDE清理项目工作区并仅使用所需的依赖项重建项目。
- (仅限Windows 操作系统)使用CCleaner工具在执行测试套件之前和之后清除所有操作系统琐事。
- (仅限 LinuxOS)在执行测试套件之前和之后释放和释放 Ubuntu/Linux Mint 中未使用的/缓存的内存。
- 如果您的基本Web Client版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和已发布版本的Web Client。
- 重新启动系统。
Test
以非 root 用户身份执行。- 始终
driver.quit()
在tearDown(){}
方法内调用以优雅地关闭和销毁WebDriver和Web Client实例。
推荐阅读
- asp.net - 如何在 ASP.Net Core 中设置全球化文化?
- sql - SQL:如何只返回 1 个以前的日期作为记录,而不是所有以前的日期
- java - dockerClient.createContainerCMD 不会将 Selenium 节点链接到 Hub
- google-cloud-platform - Cloud Run 中的证书续订
- mongodb - Mongodb pod即使处于空闲状态也会消耗内存
- loopbackjs - 在 Loopback 3 中删除基本模型关系
- python - 硒找不到元素
- sql - 查找与您输入的日期匹配的所有日期的最佳方法是什么?
- javascript - 如何用 JavaScript 重构 json 对象
- php - PHP:在带有反斜杠的双引号内使用变量