英文:
Disable popup "Restore Pages? Chrome didn't shut down correctly." in selenium webdriver Java
问题
尽管我已经看过一些相关的问题,但因为我仍然在面临这个问题,所以我再次提出这个问题。我有一个网站,在第一次登录时要求输入登录验证码,然后在以后的登录中就不再要求。如果我不使用自定义的浏览器配置文件,当Selenium运行登录时,该网站每次都会要求输入验证码。因此,我已经使用了如下的自定义浏览器配置文件(这不是问题):
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
然后,在测试完成后,我使用以下代码关闭浏览器:
driver.quit();
问题是,下次我再次运行测试时,Chrome浏览器会弹出一个窗口,显示"恢复页面?Chrome没有正确关闭。",并且该网站会在登录后再次要求输入验证码。输入验证码不是我脚本的一部分,所以脚本失败了。然后我手动点击窗口右上角的X来关闭浏览器。然后我再次运行测试,Chrome浏览器正常打开且没有弹窗,该网站不再要求输入验证码,登录成功。然后,脚本通过driver.quit()
自己关闭浏览器。然后我再次运行测试,Chrome浏览器会弹出弹窗,测试再次失败。这表明driver.quit()
没有正确关闭浏览器。
我尝试了几种设置,但弹窗仍然出现:
- 配置文件 ->
"exit_type":"none"
(以及"normal"),并且exit cleanly = true
。 - 网站设置 -> 弹出窗口和重定向 -> 阻止(推荐)。
- 我还尝试检查是否存在警报,但它显示"没有警报"。脚本无法将弹窗识别为警报。
如何确保在driver.quit()
关闭前一个测试运行的浏览器后,下一次浏览器会正常打开?请帮助解决。
以下是我的代码:我使用了Chrome扩展,所以我不能禁用扩展。
String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";
System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(extFilePath));
options.addArguments("--disable-notifications");
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
try
{
driver.switchTo().alert();
System.out.println("alert accepted");
}
catch (NoAlertPresentException Ex)
{
System.out.println("No alert");
}
driver.get("salesforce url");
我不想使用隐身模式,因为我需要保留Cookie和密码,以防止前面提到的登录验证码问题。
英文:
Though I have seen some related questions, I am putting this question again as I am still facing the issue.
I have a website that asks for login verification code on first time login and then it does not ask in further login instances. If I dont use a custom browser profile, the site asks verification code every time when selenium runs the login. Hence I have used customed browser profile as below (this is not the question)
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
Then, After the test is finished, I quit the browser using
driver.quit();
Problem is, next time I run the test again, the chrome browser opens with the popup "Restore Pages? Chrome didn't shut down correctly." and the site asks for verification code again after login. Entering verification code is not part my script, so the script fails. Then I close the browser manually by clicking on the X on the top corner.
Then I run the test again, chrome browser opens Normally without the popup and the site does not ask for verification code, login is successful. then, script closes browser itself by the driver.quit().
Then I run the test again, chrome browser opens with the popup and test fails again.
This shows that driver.quit() is not closing the browser correctly.
I have tried several settings, but still the popup is coming.
- preferences file ->
"exit_type":"none"
(as well as "normal") andexit cleanly = true
- site settings -> pop-ups and redirects -> Blocked (recommended)
- I have also tried checking alerts exists or not, but it says "No alert". Script does not detect the popup as alert.
How can I make sure the browser is opened NORMALLY every next time after driver.quit() closes the browser in the previous test run? Please help.
Below is my code: I use chrome extension, so I cannot disable extension also.
String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";
System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(extFilePath));
options.addArguments("--disable-notifications");
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
try
{
driver.switchTo().alert();
System.out.println("alert accepted");
} // try
catch (NoAlertPresentException Ex)
{
System.out.println("No alert");
}
driver.get("salesforce url");
I dont want to use incognito mode, because I need to retain the cookies and passwords to prevent the login verification code issue mentioned earlier.
答案1
得分: 4
我在设置用户目录后,通过将 exit_type 设置为 normal 在首选项中解决了这个问题。
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.exit_type", "Normal");
options.setExperimentalOption("prefs", prefs);
在你的情况下,会是这样的:
String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(extFilePath));
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
options.addArguments("--start-maximized");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.exit_type", "Normal");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
try {
driver.switchTo().alert();
System.out.println("alert accepted");
} catch (NoAlertPresentException Ex) {
System.out.println("No alert");
}
driver.get("salesforce url");
英文:
I solved this issue setting the exit_type to normal in the preferences after set up the user dir.
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.exit_type", "Normal");
options.setExperimentalOption("prefs", prefs);
In your case will be something like this
String extFilePath = "C:\\ChromeDriver\\Salesforce_inspector.crx";
System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File(extFilePath));
options.addArguments("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome\\User Data\\default");
options.addArguments("--start-maximized");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.exit_type", "Normal");
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
try
{
driver.switchTo().alert();
System.out.println("alert accepted");
} // try
catch (NoAlertPresentException Ex)
{
System.out.println("No alert");
}
driver.get("salesforce url");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论