How to handle java script pop up box in selenium -


i created automation script login contains username , password.

i have excel sheet in result updated pass if username , password correct.

but if username , password incorrect 1 javascript pop box coming.

i unable handle ok button.

i tried code. getting exception

org.openqa.selenium.webdriverexception: findelement execution failed;  open modal dialog blocked operation  (warning: server did not provide stacktrace information). 

how handle open modal dialog box?

alert alert = driver.switchto().alert(); system.out.println(alert.gettext()); alert.accept(); 

here code

public class read {      public webdriver driver;      @beforemethod     public void launch() throws exception {         system.setproperty("webdriver.chrome.driver",                 "c:\\chrome\\chromedriver_win_26.0.1383.0\\chromedriver.exe");         driver = new chromedriver();     }      @test     public void testimportexport1() throws biffexception, ioexception,             rowsexceededexception, writeexception, interruptedexception {          fileinputstream fis = new fileinputstream("data//logindev.xls");         workbook w = workbook.getworkbook(fis);         sheet s = w.getsheet(0);         string a[][] = new string[s.getrows()][s.getcolumns()];          fileoutputstream fos = new fileoutputstream("data//logindev_1.xls");         writableworkbook wwb = workbook.createworkbook(fos);         writablesheet ws = wwb.createsheet("loginresult", 0);          system.out.println("s.getrows() = " + s.getrows());          (int = 0; < s.getrows(); i++) {             system.out.println("s.getcolumns() = " + s.getcolumns());              (int j = 0; j < s.getcolumns(); j++) {                 a[i][j] = s.getcell(j, i).getcontents();                 label l = new label(j, i, a[i][j]);                 label l1 = new label(2, 0, "result");                  ws.addcell(l);                 ws.addcell(l1);                  system.out.println("labels added!!!!!!!!!");             }         }         (int = 1; < s.getrows(); i++) {             driver.get("any url");              driver.findelement(by.name("txtusername")).sendkeys(                     s.getcell(0, i).getcontents());             driver.findelement(by.name("txtpwd")).sendkeys(                     s.getcell(1, i).getcontents());             driver.findelement(by.name("btnsignin")).click();              thread.sleep(15000);              if (driver.findelement(by.linktext("xyz")).isdisplayed()) {                 system.out.println("element found");                 driver.manage().timeouts().implicitlywait(10, timeunit.seconds);                 driver.findelement(                         by.xpath("//*[@id='ctl00_headercontent_lnklogout']"))                         .click();                 thread.sleep(2000);                 label l2 = new label(2, i, "pass");                 ws.addcell(l2);             } else {                  try {                     system.out.println("element not found");                     label l2 = new label(2, i, "fail");                     ws.addcell(l2);                     alert alert = driver.switchto().alert();                     system.out.println(alert.gettext());                     alert.accept();                 } catch (noalertpresentexception e) {                     e.printstacktrace();                 }              }         }         thread.sleep(2000);         wwb.write();         wwb.close();     }  } 

i think code snippet handle modal dialog box correct. tried in c# format website works.

iwebdriver driver = new firefoxdriver(); driver.navigate().gotourl("http://www.tizag.com/javascriptt/javascriptalert.php"); driver.findelement(by.xpath("//div/form/input[@value='confirmation alert']")).click(); ialert alert = driver.switchto().alert(); console.writeline(alert.text); alert.accept(); 

what think wrong in case may alert handling code not after code line triggers modal dialog. error message have given evident doing operation on web page after modal dialog displayed , before handling it.

org.openqa.selenium.webdriverexception: findelement execution failed; open modal dialog blocked operation  

i want mention method suppress modal dialogs in c# way. use sendkeys class of name space system.windows.forms , send keyboard entries directly like,

iwebdriver driver = new firefoxdriver(); driver.navigate().gotourl("http://www.tizag.com/javascriptt/javascriptalert.php"); driver.findelement(by.xpath("//div/form/input[@value='confirmation alert']")).click(); sendkeys.sendwait("{enter}"); 

update


driver.findelement(by.name("txtusername")).sendkeys(s.getcell(0, i).getcontents()); driver.findelement(by.name("txtpwd")).sendkeys(s.getcell(1, i).getcontents()); driver.findelement(by.name("btnsignin")).click(); try{     alert alert = driver.switchto().alert();     system.out.println(alert.gettext());     alert.accept();    } catch (noalertpresentexception e) {        if (driver.findelement(by.linktext("xyz")).isdisplayed())          {          system.out.println("element found");          driver.manage().timeouts().implicitlywait(10, timeunit.seconds);          driver.findelement(          by.xpath("//*[@id='ctl00_headercontent_lnklogout']")).click();          thread.sleep(2000);          label l2 = new label(2, i, "pass");          ws.addcell(l2);         }       else          {          system.out.println("element not found");          label l2 = new label(2, i, "fail");          ws.addcell(l2);          }        } 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -