java - 无效参数异常后如何退出 ChromeDriver
问题描述
这是我当前的代码:
options.addArguments("--user-data-dir=Profile");
try {
driver = new ChromeDriver(options);
}
catch (Exception org.openqa.selenium.InvalidArgumentException) {
driver.quit
}
InvalidArgumentException 在浏览器窗口已经打开之前不会抛出,如果我尝试在 catch 块中退出它,它显然会打印一个空指针异常,因为驱动程序从未完全初始化。如何处理 InvalidArgumentException,例如正在使用的用户数据目录,同时关闭已经打开的 chromedriver 窗口。
解决方案
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.InvalidArgumentException;
WebDriver driver;
Boolean success;
ChromeOptions options = new ChromeOptions();
try {
options.addArguments("--user-data-dir=Profile");
success = true;
}
catch (InvalidArgumentException invArgEx) {
success = false;
}
if (success) {
driver = new ChromeDriver(options);
}
推荐阅读
- swift - 如何使用单例对模型控制器进行单元测试
- flutter - 将淡入淡出应用于图像或组件(模糊、渐变)
- coq - 如何一次应用于多个假设?
- excel - VBA/宏问题创建正确高度/宽度的表格并在没有电子表格最后一行的情况下创建该表格
- google-cloud-platform - 在云构建中运行 gcloud 脚本
- c# - 检查datagridview中是否存在行,如果不存在则插入新行c#
- kubernetes-apiserver - 标签的 API Swagger 定义
- python - 如何计算叠加二值图像中包含/排除的白色圆圈的数量?
- windows - 在每个输出上面写一句话( Powershell , where object 和 win32_service )
- javascript - NextJS - 读取本地文件(readdir、readFile) - 未找到模块:无法解析“fs”