question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Selenium webdriver can't get Alert Text

See original GitHub issue

Meta -

OS: Windows 10 Selenium Version:2.48 & 2.53 Browser: Chrome/Firefox Browser Version: 49.0.2623.87 (64-bit) /firefox 45

Expected Behavior -

get alert text

Actual Behavior -

Throw an exception like this: FAILED: FireFoxDriver java.lang.ClassCastException: com.google.common.collect.Maps$TransformedEntriesMap cannot be cast to java.lang.String at org.openqa.selenium.remote.RemoteWebDriver$RemoteAlert.getText(RemoteWebDriver.java:1079) at com.dbyl.tests.getAlert.FireFoxDriver(getAlert.java:54) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) at org.testng.internal.Invoker.invokeMethod(Invoker.java:648) at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:834) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1142) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:124) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) at org.testng.TestRunner.privateRun(TestRunner.java:771) at org.testng.TestRunner.run(TestRunner.java:621) at org.testng.SuiteRunner.runTest(SuiteRunner.java:357) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:352) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:310) at org.testng.SuiteRunner.run(SuiteRunner.java:259) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1176) at org.testng.TestNG.runSuitesLocally(TestNG.java:1101) at org.testng.TestNG.run(TestNG.java:1009) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

Steps to reproduce -

my test file is three html my test code:

driver =DriverFactory.getFirefoxDriver(); driver.get("file:///G:/html/index.html"); driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); driver.manage().window().maximize(); WebElement target1=driver.findElement(By.xpath("//a[@id='target1']")); String currentWindow=driver.getWindowHandle(); String target1Window = null; target1.click(); Set<String> windows = driver.getWindowHandles(); for(String window:windows) { if(!currentWindow.equals(window)) { target1Window=window; driver.switchTo().window(window); } } WebElement target2 = driver.findElement(By.id("target2")); target2.click(); windows = driver.getWindowHandles(); for(String window:windows) { if(!currentWindow.equals(window)&& !target1Window.equals(window) ) { driver.switchTo().window(window); } } Alert alert=driver.switchTo().alert(); System.out.println(alert.getText()); alert.accept();

all the html:

html.zip

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
universaljegancommented, Oct 26, 2016

Hi tobecrazy…

Refresh the page on the second browser, and click the previous alert. Then read the text of new alert, you can get.

driver.navigate().refresh(); driver.switchTo().alert().accept(); //This is the first alert, when browser 2 opens Alert alert = driver.switchTo().alert(); String browserAlertMsg = alert.getText(); //This is the second alert, when your refresh page, here you can get.

Hope this helps.

0reactions
barancevcommented, Nov 18, 2017

This scenario works well in geckodriver (tested in Firefox 57 with geckodriver 0.19). It will not be fixed in legacy Firefox driver (unless someone provides a pull-request with a patch). Please upgrade to use geckodriver.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to get the alert text using selenium webdriver
Unable to get the alert text using selenium webdriver ... I have the below code to verify the alert text and then dismiss....
Read more >
Selenium webdriver can't get Alert Text · Issue #1883 - GitHub
Refresh the page on the second browser, and click the previous alert. Then read the text of new alert, you can get. ......
Read more >
get text of alert - Selenium at Fingertips - WordPress.com
Alert is just a small box generated by Javascript code on some event performed. It gives the user some information and asks for...
Read more >
How to capture the text from Alert Message in ... - Tutorialspoint
We can capture the text from the alert message in Selenium webdriverwith the help of the Alert interface. By default, the webdriver object ......
Read more >
How to capture the text from Alert Message in ... - Edureka
How to capture the text from Alert Message in Selenium Webdriver ... You can use following command to get data from alert message:...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found