Not able to accept browser based pop up using selenium webdriver - selenium-webdriver

I'm stucked in one problem.In my site,I need to accept "Allow camera" pop up. i have tried with the below code,but i'm getting "TimeoutException" error,although the Pop up has been shown successfully,But it is not recognizing it.
try
{
WebDriverWait wait = new WebDriverWait(driver, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = driver.switchTo().alert();
System.out.println(alert.getText());
alert.accept();
}
catch (Exception e)
{
//exception handling
e.printStackTrace();
}
Please refer the attached screenshot for Pop Up.
Browser based Pop up
Thanks,

try to locate the popup and click on it or Try to implement the following code
driver.switch().alert().dismiss();

Related

When I run test in selenium using java, the alert gets closed but in code it shows failure

Adding details
In my test I am trying to click 'ok' button on the alert popup for which I am using .accept() function . and while execution , the script is able to click on ok button successfully as I can see in the application functionality but in the detailed report ,the one generated by eclipse it shows the particular test step as failure.
I am using Alert myAlert = driver.switchTo().alert();
myAlert.accept();
According to you, you are able to accept the alert but test step is showing exception in console :
You can catch the exception and proceed further with your next steps:
Below is the code :
try {
Alert myAlert = driver.switchTo().alert();
myAlert.accept();
} catch (Exception e) {
System.out.println("######### Successfully Accepted Alert ################");
}
// here proceed with your next steps.
Hope it will help you out.

Code is not getting executable after opening pop up in selenium

Please find below the code :
d.findElement(By.id("txtUserName")).clear();
d.findElement(By.id("txtUserName")).sendKeys("polarissupport#polaristest.net");
Thread.sleep(2000);
d.findElement(By.name("txtTempBox")).clear();
Thread.sleep(2000);
d.findElement(By.id("txtPassword")).sendKeys("password");
d.findElement(By.id("btnsubmit")).click();
Thread.sleep(2000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_rdoAdmin")).click();
Thread.sleep(2000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_lnkContinue")).click();
Thread.sleep(5000);
//GC Inbox
String s1 = "CHR-665";//d.findElement(By.id("ctl00_ContentPlaceHolder1_lblrequestidtext")).getText(); //For taking Request Id
String tem[] = s1.split("-");
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_txtGrantID")).sendKeys(tem[1]);
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_lnkSearch")).click();
Thread.sleep(5000);
d.findElement(By.id("ctl00_ContentPlaceHolder1_ctlCommonInbox_grdAdminGrantCHR_ctl00_ctl04_btnAction")).click();
Thread.sleep(8000);
d.findElement(By.xpath("//*[#id='ctl00_ContentPlaceHolder1_lnkReleasePayment']")).click();
After the last line which opens the Pop-up, code is not getting executable to handle this opened Pop-up.
Please find below the code through which I handled the Pop-up:
String MainWindow=d.getWindowHandle();
Set<String> set=d.getWindowHandles();
Iterator<String> it=set.iterator();
while(it.hasNext())
{
System.out.println("Inside While");
String ChildWindow=it.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{
d.switchTo().window(ChildWindow);
d.close();
}
}
System.out.println("Outside While");
d.switchTo().window(MainWindow);
Please let me know how to execute Pop-up handling code when Pop-up is opened because when I close the Pop-up manually then only this Pop-up handling code executes not at the time when Pop-up is opened.
Please let me know if question doesn't provide satisfactory information.
Thanks
Try Using Alert class or PopupWindowFinder Code in the link is based on c#, i hope you can convert it to your desired language easily
popup's in selenium webdrivers

Wait for pop up window to close in selenium webdriver

Is there any command to wait for the popup to close.nActually my project is like on home page popup will appear and after choosing city pop gets close. Then I am able to click on sign in button. But when i run the script, before closing the pop up my next command which is click on sign in button are executing. So test script getting fail. When I used sleep (10000),then my script is working fine. But every time i don't want to use sleep,
Enter image description here
Please help me with your answers
Thanks
I would wait for the popup to be hidden :
WebDriverWait wait = new WebDriverWait(driver, 5000); // 5 seconds timeout
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.cssSelector("popup locator")));
One way to loop over and over rather than a fixed wait:
WebDriver driver = new FirefoxDriver();
//Accept the expected alert
driver.switchTo().alert().accept();
//Wait dynamically to disappear
int i=0;
while(i<15)
{
try{
driver.switchTo().alert();
Thread.sleep(2000);
}catch(NoAlertPresentException a){
break;
}
catch (Exception e) {
}
i++;
}

How to handle unpredictable and unwanted pop up appears in selenium webdriver even t is not expected behavior

In our application we are getting pop up some times but it is not expected behavior, once this pop up displayed remaining scripts will get fail as focus is on pop up. So can you please guide me to resolve this issue
If it is happening in the same place a lot you can deal with it like this...
public boolean isAlertPresent() {
try {
driver.switchTo().alert();
return true;
} // try
catch (Exception e) {
return false;
} // catch
}
Now, you can check whether alert is present or not by using the method written above as below:
if (isAlertPresent()) {
driver.switchTo().alert();
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
}
I wouldn't put this everywhere though because it will really slow down the tests.
What you need to do is -
Create a function that will be used to access any element for ex-
getButton("xpath of your desired xpath").click();
getButton(String Xpath){
if(driver.findElement(By.xpath()).isVisible)
return driver.findElement(By.xpath())
else{
if (isAlertPresent()) {
driver.switchTo().alert();
driver.switchTo().alert().accept();
driver.switchTo().defaultContent();
}
return driver.findElement(By.xpath())
}
}
MAke sure you always make call to this function to access any element from UI .

How can I check a checkbox on the popup window? Selenium Webdriver

When a popup window open how can I handle on this window?
driver.switchTo().defaultContent();
MenuUtil.doClickMenu(selenium, driver, "Access 2G", "Cảnh báo", "Giám sát cảnh báo BTS");
ControlBase.waitForLoadOk(5);
CheckBoxUtil.doCheckByXpath(selenium, "//*[#id='listRow1']/tbody/tr[1]/td[18]/input");
ControlBase.waitForLoadOk(5);
ButtonUtil.doClickByLabel(driver, "Cập nhật");
ControlBase.waitForLoadOk(5);
CheckBoxUtil.doCheckByXpath(selenium, "//*[#id='txtName']");
Generally I use the Selenium IDE. Here you can 'click & record', which allows you to then get the XPath of the JS pop-up alert box.
I would suggest you first check for the alert box showing up with something such as this:
if (selenium.isAlertPresent()){
System.out.println(selenium.getAlert()); //show alert text message
selenium.click("id=alertbox_ok_btn"); //click OK on alert box
}
Download Selenium IDE from here: http://docs.seleniumhq.org/download/
Also, refer to Selenium's documentation for Java here: http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html
Try the below code written in JAVA
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("Your application URL");
String parentWindow = driver.getWindowHandle();
//click on the link/box/whatever to open the pop up
driver.findElement(By.xpath("")).click();
Set <String> popupWindows = driver.getWindowHandles();
for(String popup : popupWindows){
driver.switchTo().window(popup);
//you can use getTitle/getCurrentUrl to match with title/url of the pop up
if(driver.getTitle().equals("actual title on the pop up")){
break;
}
}
//Write a code to check a checkbox
//Close the pop up
driver.close();
//Come back to the parent window
driver.switchTo().window(parentWindow);
//Code if you want to test/do something else
driver.close();

Resources