Aakash Goyal
Aakash Goyal

Reputation: 305

Java Selenium - Unable to reach to Alert with both Chrome and firefox

I am new to Selenium and I have written following code to print alert text and accept alert.

public class AlertPractice {

 public static void main(String[] args) throws InterruptedException {

    WebDriver driver  = new FirefoxDriver();
    driver.get("http://output.jsbin.com/usidix/1");

    driver.findElement(By.cssSelector("input[value=\"Go!\"]")).click();
    Thread.sleep(1000);
    String S = driver.switchTo().alert().getText();
    Thread.sleep(1000);
    driver.switchTo().alert().accept();

    System.out.println(S);
    driver.close();
}

On running with FIREFOX driver I get following exception :

org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output....

and on Running with Chrome Driver I get below exceptions:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status from unexpected alert open (Session info: chrome=53.0.2785.101) (Driver info: chromedriver=2.21.371459 ...

Any help would be appreciable.

Upvotes: 0

Views: 1086

Answers (2)

Aakash Goyal
Aakash Goyal

Reputation: 305

Seems like an issue with The Webdriver version for Selenium in case of Firefox and Chromedriver version for Chrome. Downloaded and applied latest beta version for selenium and latest chromedriver for chrome. Works fine now.

Upvotes: 1

Saurabh Gaur
Saurabh Gaur

Reputation: 23805

To launch latest Mozilla Firefox, you need to download latest geckodriver executable and then follow this answer that how to launch latest firefox using selenium.

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status from unexpected alert open

Actually there is no need to switch Alert two times to getting alert text and then accept. You can do this both task using single switching to Alert.

For better way, you should try to switch Alert using WebDriverWait to wait until alert is present instead of Thread.sleep() as below :-

driver.findElement(By.cssSelector("input[value='Go!']")).click();

WebDriverWait wait = WebDriverWait(driver, 10);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());

String S = alert.getText();
System.out.println(S);

alert.accept();

Upvotes: 0

Related Questions