I am trying to Automate a web application , where I click on to next page and it throws a confirmation message which I could not handle it
I have used the below in my script
WebDriverWait wait = new WebDriverWait(Driver, 15);
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();
Error : Expected condition failed: waiting for alert to be present
So I think when you click on next button it open a new tab and displays
the confirmation JavaScript alert.
You can handle this for now only through Firefox geckoDriver this
is issue with the [ChromeDriver][1]
https://github.com/SeleniumHQ/selenium/issues/9040
I've tried like below, Just check it
WebDriver driver = new FirefoxDriver();
driver.navigate().
to("https://www.w3schools.com/jsref/event_onload.asp");
driver.manage().window().maximize();
WebElement element = driver.findElement(By.xpath(".
(//a[contains(text(),'Try it Yourself ยป')])[1]"));
Set<String> originalWindowHandles =driver.getWindowHandles();
((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
Set<String> updatedWindowHandles = driver.getWindowHandles();
for(String window: updatedWindowHandles)
{
if(!originalWindowHandles.contains(window)){
driver.switchTo().window(window);
break;
}
}
Thread.sleep(5000);
Alert alert = driver.switchTo().alert();
alert.accept();
}
Related
i want run below code in edge browser using java selenium project
WebDriverWait wait = new WebDriverWait(driver, waitSeconds);
// wait for page complete
//log.debug("PageLoadState>>"+pageLoadCondition.toString());
// Wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState").toString()
.equals("complete");
JavascriptExecutor js = (JavascriptExecutor) getDriver();
boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
// Wait Javascript until it is Ready!
if (!jsReady) {
// System.out.println("JS in NOT Ready!");
wait.until(jsLoad);
}
we are getting exception in below line as Exception class:org.openqa.selenium.JavascriptException
the reason is:org.openqa.selenium.JavascriptException: javascript error: Function is not a constructor
boolean jsReady = (Boolean) js.executeScript("return document.readyState").toString().equals("complete");
We set edgeoption as below
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setBrowserName("MicrosoftEdge");
EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.setCapability("ms:inPrivate", true);
// edgeOptions.setCapability("prefs", edgePrefs);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.merge(desiredCapabilities);
edgeOptions.setPageLoadStrategy("eager");
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
edgeOptions.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
driver = new EdgeDriver(edgeOptions);
Pls advise is any other way to run the javascript in edge using java and selenium
I have an automation scenario to perform a content searching. The problem happen in the search result screen where there are 2 Javascript alert popup appears at nearly the same time. I said nearly because the popup window appears one by one, not all of them at once. I managed to handle the first one, but not with the second one. NoAlertPresentException happened for the second alert handler code.
Here's where the alert happened:
public WSSPage enterAndSearchContent(String title) {
waitWSSPageLoaded();
waitForElementTextWithEnter(searchTextField, title);
waitWSSPageLoaded();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss();
wait.until(ExpectedConditions.alertIsPresent()); //NoAlertPresentException happened here
Alert alert2 = driver.switchTo().alert();
alert2.dismiss();
return PageFactory.initElements(driver, WSSPage.class);
}
I alse tried using alert.dismiss(); twice but still it gives NoAlertPresentException at the second alert.dismiss(); like the below code:
public WSSPage enterAndSearchContent(String title) {
waitWSSPageLoaded();
waitForElementTextWithEnter(searchTextField, title);
waitWSSPageLoaded();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
alert.dismiss();
alert.dismiss(); //NoAlertPresentException happened here
return PageFactory.initElements(driver, WSSPage.class);
}
When I try manually dismissing the alert, the second alert appears almost instantly. I guess the problem is the code can't catch the second alert because it was too fast?
Is there any workaround for this problem? Thank you.
Note:
I'm using Firefox
i am using WebDriverWait in Selenium, i want to wait 10 second to wait if some Alert or Confirm box will appear, if it appears i will accept it. for example if confirm box ask me to "Leave Page" or "Stay on Page" then i want to leave page automatically, and if alert is not appeared in 10 seconds then continue code Excecution instead of throwing Exception. or you can tell me some way to accept All confirm, alert boxes automatically during whole selenium test. thanks
here is my code
WebDriver driver = new FirefoxDriver();
driver.get("https:www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 10 );
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("alert was not present");}
else{
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println("alert was present");}
try/catch is useful to catch/handle the exception. Just place alert code in try block, if any exception occurs, it will handle in catch block and there after execution will continue..
WebDriver driver = new FirefoxDriver();
driver.get("https:www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 5 );
try{
if(wait.until(ExpectedConditions.alertIsPresent())==null)
{
System.out.println("alert was not present");
}else{
Alert alert = driver.switchTo().alert();
alert.accept();
System.out.println("alert was present");
}
}catch(Exception e){
System.out.println("ignored alret not present exception" +e.getMessage());
}
}
Thanks
I am trying to automate gmail sending a email using selenium i am using phantom js (For headless)
I am getting the following Exception
org.openqa.selenium.remote.UnreachableBrowserException Error communicating with the remote browser
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability("takesScreenshot", true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"C:\\jars\\phantomjs-2.0.0-windows\\bin\\phantomjs.exe"
);
//File file = new File("C:/jars/phantomjs-2.0.0-windows/bin/phantomjs.exe");
// System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver d = new PhantomJSDriver(caps);
//WebDriver d=new HtmlUnitDriver();
WebDriverWait wait = new WebDriverWait(d, 10);
d.get("https://www.gmail.com/intl/en/mail/help/about.html");
System.out.println("navigated to gmail");
d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
d.manage().window().setSize(new Dimension(1576, 798));;
d.findElement(By.id("gmail-sign-in")).click();
System.out.println("Clicked on Signin");
Thread.sleep(9000);
d.findElement(By.id("Email")).sendKeys("chaitanyapujari97#gmail.com");
System.out.println("Entered Email");
Thread.sleep(9000);
d.findElement(By.id("next")).click();
System.out.println("Clicked next");
d.findElement(By.id("Passwd")).sendKeys("your pwd");
System.out.println("Entered password");
Thread.sleep(9000);
d.findElement(By.id("signIn")).click();
System.out.println("Clicked on signin");
Thread.sleep(9000);
d.findElement(By.xpath("html/body/div[7]/div[3]/div/div[2]/div[1]/div[1]/div[1]/div[2]/div/div/div[1]/div/div")).click();
System.out.println("Clicked on Compose email");
//Thread.sleep(9000);
Thread.sleep(9000);
d.findElement(By.name("to")).sendKeys("your email");
System.out.println("Entered To address");
Thread.sleep(9000);
d.findElement(By.name("q")).click();
d.findElement(By.name("subjectbox")).sendKeys("PHANTOm Js");
System.out.println("Entered Subject");
//WebElement webElement=d.findElement(By.name("subjectbox"));
/*String keysPressed = Keys.chord(Keys.CONTROL, Keys.RETURN);
WebElement element=d.findElement(By.xpath("html/body"));
element.sendKeys(keysPressed) ;*/
Thread.sleep(9000);
d.findElement(By.xpath("html/body/div[14]/div/div/div/div[1]/div[3]/div[1]/div[1]/div/div/div/div[3]/div/div/div[4]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/div/div/div[4]/table/tbody/tr/td[1]/div/div[2]")).click();
System.out.println("Clicked On send");
i am able to add the recipient email address and subject unable to click on send button.
I am not able to figure it out i am new to Headless Selenium automation please help,
Please try this , I hope it will help you , Why are you using such long XPATH , it will have to traverse from html.
d.findElement(By.xpath(".//tr[#class='n1tfz']/td[1]/div[1]/div[2]")).click();
I found it the problem was with the jar i was using the jar phantomjsdriver-1.1.0.jar i have changed the versions of jar now which is phantomjsdriver-1.2.1.jar works perfectly fine.
Thanks for the help friends.
Does anyone know how to disable this? Or how to get the text from alerts that have been automatically accepted?
This code needs to work,
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
Alert alert = driver.switchTo().alert();
alert.accept();
return alert.getText();
but instead gives this error
No alert is present (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 2.14 seconds
I am using FF 20 with Selenium 2.32
Just the other day i've answered something similar to this so it's still fresh. The reason your code is failing is if the alert is not shown by the time the code is processed it will mostly fail.
Thankfully, the guys from Selenium WebDriver have a wait already implemented for it. For your code is as simple as doing this:
String alertText = "";
WebDriverWait wait = new WebDriverWait(driver, 5);
// This will wait for a maximum of 5 seconds, everytime wait is used
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to alert() something
wait.until(ExpectedConditions.alertIsPresent());
// Before you try to switch to the so given alert, he needs to be present.
Alert alert = driver.switchTo().alert();
alertText = alert.getText();
alert.accept();
return alertText;
You can find all the API from ExpectedConditions here, and if you want the code behind this method here.
This code also solves the problem because you can't return alert.getText() after closing the alert, so i store in a variable for you.
Before you accept() the alert you need to get the text. What you're doing right now is accepting (clicking "OK") on the alert then trying to get the alerts text after it's out of the screen, i.e. no alert present.
Try the following, I just added a String that retrieves the alert text then return that string instead.
driver.findElement(By.xpath("//button[text() = \"Edit\"]")).click();//causes page to
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.accept();
return alertText;
Selenium webdriver does not wait for the alert.
So it will try to switch to a non-existent alert and thats why it fails.
For a quick and not so good fix, put in a sleep.
A better solution would be to implement your own wait for alert method, before trying to switch to the alert.
UPDATE
Something like this, copy pasted from here
waitForAlert(WebDriver driver)
{
int i=0;
while(i++<5)
{
try
{
Alert alert3 = driver.switchTo().alert();
break;
}
catch(NoAlertPresentException e)
{
Thread.sleep(1000)
continue;
}
}
}
Following method with synchronized option will add more stability
protected Alert getAlert(long wait) throws InterruptedException
{
WebDriverWait waitTime = new WebDriverWait(driver, wait);
try
{
synchronized(waitTime)
{
Alert alert = driver.switchTo().alert();
// if present consume the alert
alert.accept();
return alert;
}
}
catch (NoAlertPresentException ex)
{
// Alert not present
return null;
}
}
Here is the JavaScript answer. The documentation has examples for all languages. https://www.selenium.dev/documentation/en/webdriver/js_alerts_prompts_and_confirmations/
await driver.wait(until.alertIsPresent());
el = driver.switchTo().alert();
await el.accept();