How to handle Alerts in Selenium WebDriver

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 พ.ค. 2020
  • In this video, I have shown how to handle Alerts in Selenium WebDriver.
    Selenium WebDriver is an open-source tool for automated testing of web apps across many browsers.
    WebDriver provides an API for working with the three types of native popup messages offered by JavaScript. These popups are styled by the browser and offer limited customization.
    1) Alerts (Simple Alerts)
    The simplest of all javascript popups is referred to as an alert, which shows a custom message, and a single button that dismisses the alert, labeled in most browsers as OK. It can also be dismissed in most browsers by pressing the close button, but this will always do the same thing as the OK button.
    WebDriver can get the text from the popup and accept or dismiss these alerts.
    ------- Example -------
    //navigate to the website
    driver.get("www.hyrtutorials.com/p/alerts...");
    //Click the link to activate the alert
    driver.findElement(By.id("alertBox")).click();
    //Wait for the alert to be displayed and store it in a variable
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    //Store the alert text in a variable
    String text = alert.getText();
    //Press the OK button
    alert.accept();
    2) Confirm (Confirmation Alerts)
    A confirm box is similar to an alert, except the user can also choose to cancel the message.
    ------- Example -------
    //navigate to the website
    driver.get("www.hyrtutorials.com/p/alerts...");
    //Click the link to activate the alert
    driver.findElement(By.id("confirmBox")).click();
    //Wait for the alert to be displayed and store it in a variable
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    //Store the alert text in a variable
    String text = alert.getText();
    //Press the Cancel button
    alert.dismiss();
    3) Prompt (Prompt Alerts)
    Prompts are similar to confirm boxes, except they also include a text input. Similar to working with form elements, you can use WebDriver’s send keys to fill in a response. This will completely replace the placeholder text. Pressing the cancel button will not submit any text.
    ------- Example -------
    //navigate to the website
    driver.get("www.hyrtutorials.com/p/alerts...");
    //Click the link to activate the alert
    driver.findElement(By.id("promptBox")).click();
    //Wait for the alert to be displayed and store it in a variable
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    //Type your message
    alert.sendKeys("Hello Tester");
    //Press the OK button
    alert.accept();
    The operations that we can perform on these popups using selenium are,
    1) Accept - Refers to clicking on the Ok button.
    2) Dismiss - Refers to clicking on the Cancel button.
    3) SendKeys - Refers to entering text into a textbox available in the prompt box.
    4) GetText - Refers to get the label text present on the popup box.
    Webpage used for practice in this video is as follows:
    www.hyrtutorials.com/p/alerts...
    ==============================================
    ************* Checkout my other playlists *************
    ==============================================
    Java Programming videos playlist:👇
    🔗 bit.ly/3keRJGa
    Selenium WebDriver with Java videos playlist:👇
    🔗 bit.ly/2FyKvxj
    Selenium interview questions videos playlist:👇
    🔗 bit.ly/3matUB3
    Windows automation with FlaUI videos playlist:👇
    🔗 bit.ly/33CG4dB
    CSS Selectors videos playlist:👇
    🔗 bit.ly/2Rn0IbD
    XPath videos playlist:👇
    🔗 bit.ly/2RlLdkw
    Javascript Executor videos playlist:👇
    🔗 bit.ly/2FhNXwS
    Apache POI videos playlist:👇
    🔗 bit.ly/2RrngrH
    Maven videos playlist:👇
    🔗 bit.ly/2DYfYZE
    How to fix Eclipse errors:👇
    🔗 bit.ly/3ipvNYf
    ==============================================
    ==============================================
    Connect us @
    🔗 Website - www.hyrtutorials.com
    🔗 Facebook - HYRTutorials
    🔗 LinkedIn - www.linkedin.com/company/hyrtutorials
    🔗 Twitter - Hyrtutorials
    ==============================================
    ==============================================
    🙏 Please Subscribe🔔 to start learning for FREE now, Also help your friends in learning the best by suggesting this channel.
    #hyrtutorials #selenium #alerts #webAutomation
    how to handle alerts in selenium,selenium webdriver,selenium webdriver tutorial,alerts in selenium webdriver,alerts/popups in selenium,handling alerts in selenium,handling pop-ups in selenium,how to handle javascript alerts,types of alerts in selenium,simple alert,prompt alert,confirmation alert,how to handle popup using seleium webdriver,handling popups,how to get alert text in selenium webdriver,javascript popup box

ความคิดเห็น • 72

  • @HYRTutorials
    @HYRTutorials  4 ปีที่แล้ว +1

    Please use the below link for the selenium videos playlist:
    🔗 bit.ly/2FyKvxj

  • @srija_sk
    @srija_sk 2 ปีที่แล้ว +2

    Thanks for the detailed explanation, Sir!

  • @C.Sakthivignesh
    @C.Sakthivignesh ปีที่แล้ว +2

    Thank you so much sir for made this video.

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว +1

      Happy learning buddy 😊

  • @mohamedkaleemullah1161
    @mohamedkaleemullah1161 8 หลายเดือนก่อน

    Very clearly explained

  • @factsofallahal-shaheedserv7816
    @factsofallahal-shaheedserv7816 2 ปีที่แล้ว +1

    If you find this video is not clear, you can watch this video with good , the link is in 1st comment.
    The content of the video is good, explained in simple way

  • @MuhammadRizwan-zu4tr
    @MuhammadRizwan-zu4tr 2 ปีที่แล้ว +3

    Well explained for the initial level users.

  • @Allinoneclick23
    @Allinoneclick23 ปีที่แล้ว

    thank you for making video

  • @pankajkore1420
    @pankajkore1420 2 ปีที่แล้ว +1

    Thank you sir..!

  • @ankitshrivastava1772
    @ankitshrivastava1772 3 ปีที่แล้ว +1

    well explained.

  • @Od3o_ela_sabil_rabek
    @Od3o_ela_sabil_rabek 2 ปีที่แล้ว +1

    After interacting with the alert how to return focus on the main window?

    • @HYRTutorials
      @HYRTutorials  2 ปีที่แล้ว

      It automatically happens buddy

  • @kkartkk9372
    @kkartkk9372 3 ปีที่แล้ว +1

    Hi
    Thanks for the video.
    I have a query
    For the prompt alert a default text was displayed in the website.so unable to clear the text and type the text thru sendkeys method.
    Could you please explain on how to resolve this?
    Also is there any method to read the default text from the prompt alert

    • @HYRTutorials
      @HYRTutorials  3 ปีที่แล้ว

      No need to clear the text.
      when u enter the text using sendkeys method, it automatically clears the old one and enters new text.

    • @kkartkk9372
      @kkartkk9372 3 ปีที่แล้ว

      @@HYRTutorials t
      Thanks for the response.
      I tried the method sendkeys but it's not working .also pl tell me how to read the default text in that box

    • @HYRTutorials
      @HYRTutorials  3 ปีที่แล้ว

      @@kkartkk9372 sendkeys method will actually enter the text into that prompt but in the ui u can't c d difference.
      We can't get the text present inside that textbox but we can get the message text.

  • @ManiKandan-vo2qr
    @ManiKandan-vo2qr 4 ปีที่แล้ว

    Can we handle this alert using the Driver capabilities as i could see there is method called "setUnhandledPromptBehaviour" . Please let me know

    • @HYRTutorials
      @HYRTutorials  4 ปีที่แล้ว

      The alerts are supposed to be handled programmatically bcz every alert is having a purpose or meaning to it.

  • @dev9559
    @dev9559 10 หลายเดือนก่อน +1

    what if we have multiple text boxes in alert?

    • @HYRTutorials
      @HYRTutorials  9 หลายเดือนก่อน

      Mostly it won't be there buddy because that's how the alerts are designed

  • @ankitshrivastava1772
    @ankitshrivastava1772 3 ปีที่แล้ว +1

    In Propmt alert "Yada Giri Reddy" is already present and in my script when i use sendkeys the it not working.

    • @HYRTutorials
      @HYRTutorials  3 ปีที่แล้ว

      We don't see the change of text but after clicking on ok button, you can see whether the name is updated or not from the label

  • @kiranbabuchava7390
    @kiranbabuchava7390 2 ปีที่แล้ว +1

    Hi Reddy, Any Rest assured API testing?

    • @HYRTutorials
      @HYRTutorials  2 ปีที่แล้ว

      Nope, atleast not in next 3 months

  • @nannapanenieaswar3097
    @nannapanenieaswar3097 2 ปีที่แล้ว +1

    sir i am not getting confirmation(&)prompt alerts. it showing me an error called "Alerts.java"

    • @HYRTutorials
      @HYRTutorials  2 ปีที่แล้ว

      Paste the complete error here or send me the screenshots on hyadagirireddytutorials@gmail.com

  • @Shivanibbbbb
    @Shivanibbbbb 2 ปีที่แล้ว

    please help me, i am trying to get button on form
    its open separately when i click on add to cart button
    its not in nor i can get it from alert .
    i am clicking on product add to cart button .when i click its open separate form which show you add quantity and add to cart button

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      So what exactly is the problem here buddy?

  • @SaiKiran-gz9sq
    @SaiKiran-gz9sq 11 หลายเดือนก่อน +1

    Hi sir iam not getting the confirmation and prompt alerts also it is not accept the Ok button after getting the simple alert what should I do sir

    • @HYRTutorials
      @HYRTutorials  11 หลายเดือนก่อน

      That's strange. Are you getting any errors buddy?

  • @ArunRajpidige
    @ArunRajpidige 4 ปีที่แล้ว

    I have to test a scenario where i need to handle the browser alert while closing the tab in selenium grid.. Im using the same steps as you mentioned But Browser is closing and i cant handle the alert. Can you help me with this

    • @HYRTutorials
      @HYRTutorials  4 ปีที่แล้ว

      U want to handle the alert first then close the browser?

    • @ArunRajpidige
      @ArunRajpidige 4 ปีที่แล้ว

      @@HYRTutorials ok i will explain the manual steps..step1: I navigate to some page step2:Then i will click close browser icon step3: then popup will come saying are you sure to leave.. i need to verify that popup now

    • @HYRTutorials
      @HYRTutorials  4 ปีที่แล้ว

      @@ArunRajpidige for closing the browser what u r using in selenium?

    • @ArunRajpidige
      @ArunRajpidige 4 ปีที่แล้ว

      @@HYRTutorials yes i'
      m using selenium

    • @HYRTutorials
      @HYRTutorials  4 ปีที่แล้ว

      @@ArunRajpidige I mean driver.close() or driver.quit() ?

  • @factsofallahal-shaheedserv7816
    @factsofallahal-shaheedserv7816 2 ปีที่แล้ว +1

    Sir @ prompt alert you didn't change heading in comment line, might be you forgot

  • @yeliza2350
    @yeliza2350 2 ปีที่แล้ว +2

    27 February ...it s done🌍

  • @ayajshiekh3205
    @ayajshiekh3205 2 ปีที่แล้ว +1

    Why we are not using
    Alert a= new Alert(driver)

    • @HYRTutorials
      @HYRTutorials  2 ปีที่แล้ว

      You can use that as well.
      Here I'm not using it because I m performing only a single action on the alert. So we don't need to store the instance of that alert

  • @vinayakmore7030
    @vinayakmore7030 ปีที่แล้ว

    Hi..We have similar requirement... Could you please modify your program,,,Instead of manually clicking click me button..we need to listen the website and website will send confirmation box to click Yes/No button We need to act on those button...no need to close the browser..Program will wait for next alert..How to do this?

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      in order to do that we need to run some infinite loop and listen to the event buddy.

  • @ramanamohan9813
    @ramanamohan9813 2 ปีที่แล้ว +1

    How to handle noticifications. I mean allowing/blocking noticification .

    • @HYRTutorials
      @HYRTutorials  2 ปีที่แล้ว

      Checkout this video: th-cam.com/video/deKXbbIHD4w/w-d-xo.html

  • @minakshigulia8997
    @minakshigulia8997 4 ปีที่แล้ว +1

    how to handle drop down present on pop-up??????????

    • @HYRTutorials
      @HYRTutorials  4 ปีที่แล้ว

      I think you could inspect the drop-down on the pop-up also ryt?
      If so it's the same way how we handle normal drop-down.

    • @minakshigulia8997
      @minakshigulia8997 4 ปีที่แล้ว +1

      Thnku sir

  • @-VMamatha
    @-VMamatha ปีที่แล้ว

    How to write alerts in pom file can anyone explain me please

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      Do u mean page object model or maven pom.xml file?

  • @VenkataRamana-tw6lj
    @VenkataRamana-tw6lj ปีที่แล้ว +1

    Anna oka page nunchi inkoka page ki velletappudu different different advertisements open avuthunnayi vatini yela remove (overcome) cheyali anna

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      Try using the direct url buddy.

  • @nityapooja477
    @nityapooja477 ปีที่แล้ว

    Can u please share me the pop up related link

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      All the links were updated in the description of the video buddy

  • @f3century565
    @f3century565 ปีที่แล้ว

    i whant to open multiple windows

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      With selenium version 4 you can do that buddy

  • @paulraj6567
    @paulraj6567 ปีที่แล้ว +1

    Sir please not using of id and Name . Can u please xpath locators to explain

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      XPath | XML Path Language |: th-cam.com/play/PLacgMXFs7kl-w3RW5IN0eWVIHsYV8uHdz.html

  • @harshinikalyan6457
    @harshinikalyan6457 ปีที่แล้ว +1

    Anyone got issue with Thread.sleep(2000); my script get stuck while using thread otherwise working

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      Alerts use chesetappudu thread.sleep isthe problem osthundi buddy

    • @HYRTutorials
      @HYRTutorials  ปีที่แล้ว

      Not always but most of the times

    • @dj_evanzz_official
      @dj_evanzz_official ปีที่แล้ว

      Try to use wait()