-2

I am automating a sharepoint website using webdriver and java, which contains javascript embedded into the button code.

Please find the HTML snippet of the same :

<input id="ctl00_ContentPlaceHolder1_btnDelete" class="btn" type="submit" onclick="javascript:return confirm('Please select OK to proceed else Cancel.');" value="Delete" name="ctl00$ContentPlaceHolder1$btnDelete"/>

Please find my code to handle the same :

String js = "if (window.alert.myAlertText == undefined) {window.alert.myAlertText = null;  window.alert = function(msg){ window.alert.myAlertText = msg; };}";
//Click delete button 
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.findElement(By.xpath(".//*@id='ctl00_ContentPlaceHolder1_btnDelete']")));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", driver.switchTo().alert(), js);

But i am not able to accept or dismiss alert.

Kindly help!!!!

Snapshot of the button :

Snapshot Alert

Suyash Nande
  • 59
  • 1
  • 1
  • 9

1 Answers1

1

You can switch to alert by using following and accept

Alert alert = driver.switchTo().alert();
alert.accept();

Also, to dismiss you can refer this

Alert alert = driver.switchTo().alert();
alert().dismiss();
  • That never works... coz there is javascript embedded into it. – Suyash Nande Jun 26 '16 at 04:33
  • This should work and is the proper way to handle the dialog you have pictured. The JS is what is calling the dialog and that's why you can handle it using the code above. You probably need to wait briefly for the dialog to appear. – JeffC Jun 27 '16 at 02:49
  • It's not working..... any alternative would help? – Suyash Nande Jun 27 '16 at 05:22