生活随笔
收集整理的這篇文章主要介紹了
Selenium对于对话框alert,confirm,prompt的处理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
html 源碼:
??? <html> ?
???? ?
??????? <head> ?
???? ?
??????????? <title>Alert</title> ?
<script>
function myFunction()
?{
?? ?var x;
?? ?var b = prompt("hello","harry potter");
??? if (b!=null && b!="") {
?? ?
?? ?x = "hello"+b+"歡迎你";
?? ?
??? ?}
?? ?document.getElementById("display").innerHTML=x;
?}
</script>
????
??????? </head> ?
???? ?
??????? <body> ?
???? ?
?????? ??? ?<input id = "alert" value = "alert" type = "button" onclick = "alert('歡迎!請按確認繼續!');"/> ?
?????? ??? ?<input id = "confirm" value = "confirm" type = "button" onclick = "confirm('確定嗎?');"/> ?
?????? ??? ?<input id = "prompt" value = "prompt" type = "button" onclick = "myFunction()" /> ?
?? ? ? ?? ? <p id = "display"></p>
???????? ?
???? ?
??????? </body> ?
???? ?
??? </html>?
?以上html代碼在頁面上顯示了三個按鈕,點擊他們分別彈出alert、confirm、prompt對話框。如果在prompt對話框中輸入文字點擊確定之后,將會刷新頁面,顯示出這些文字
import?org.openqa.selenium.Alert;?? import?org.openqa.selenium.By;?? import?org.openqa.selenium.WebDriver;?? import?org.openqa.selenium.firefox.FirefoxDriver;?? ?? public?class?DialogsStudy?{?? ?? ????? ?? ????public?static?void?main(String[]?args)?{?? ?????????? ????????System.setProperty("webdriver.firefox.bin","D:/Program Files (x86)/Mozilla Firefox/firefox.exe");???? ????????WebDriver?dr?=?new?FirefoxDriver();?? ????????String?url?=?"file:///C:/Users/Administrator/Desktop/test.htm"; ????????dr.get(url);?? ?????????? ?????????? ????????dr.findElement(By.id("alert")).click();?? ????????Alert?alert?=?dr.switchTo().alert();?? ????????String?text?=?alert.getText();?? ????????System.out.println(text);?? ????????alert.dismiss();?? ?????????? ?????????? ????????dr.findElement(By.id("confirm")).click();?? ????????Alert?confirm?=?dr.switchTo().alert();?? ????????String?text1?=?confirm.getText();?? ????????System.out.println(text1);?? ????????confirm.accept();?? ?????????? ?????????? ????????dr.findElement(By.id("prompt")).click();?? ????????Alert?prompt?=?dr.switchTo().alert();?? ????????String?text2?=?prompt.getText();?? ????????System.out.println(text2);?? ????????prompt.sendKeys("jarvi");?? ????????prompt.accept();?? ?????????? ????}?? ?? }?
轉載于:https://www.cnblogs.com/wuyepiaoxue/p/5661187.html
總結
以上是生活随笔為你收集整理的Selenium对于对话框alert,confirm,prompt的处理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。