How to handle Alerts in Selenium WebDriver

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • 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.hyrtutoria...");
    //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.hyrtutoria...");
    //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.hyrtutoria...");
    //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.hyrtutoria...
    ==============================================
    ************* 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

КОМЕНТАРІ • 73

  • @HYRTutorials
    @HYRTutorials  4 роки тому +1

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

  • @Meetelugodivlogs
    @Meetelugodivlogs 23 години тому

    Hello sir i have one doubt
    Actually i am doing prompt alert while that time im unable to re enter the text in prompt alert text field i cant able clear the text which is already present how can i do that sir

  • @Od3o_ela_sabil_rabek
    @Od3o_ela_sabil_rabek 2 роки тому +1

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

  • @C.Sakthivignesh
    @C.Sakthivignesh Рік тому +2

    Thank you so much sir for made this video.

  • @SaiKiran-gz9sq
    @SaiKiran-gz9sq Рік тому +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  Рік тому

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

  • @srija_sk
    @srija_sk 2 роки тому +2

    Thanks for the detailed explanation, Sir!

  • @dev9559
    @dev9559 Рік тому +1

    what if we have multiple text boxes in alert?

    • @HYRTutorials
      @HYRTutorials  Рік тому

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

  • @VenkataRamana-tw6lj
    @VenkataRamana-tw6lj Рік тому +1

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

  • @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

  • @factsofallahal-shaheedserv7816
    @factsofallahal-shaheedserv7816 2 роки тому +1

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

  • @kiranbabuchava7390
    @kiranbabuchava7390 2 роки тому +1

    Hi Reddy, Any Rest assured API testing?

    • @HYRTutorials
      @HYRTutorials  2 роки тому

      Nope, atleast not in next 3 months

  • @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.

  • @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

  • @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  2 роки тому

      So what exactly is the problem here buddy?

  • @mohamedkaleemullah1161
    @mohamedkaleemullah1161 Рік тому

    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 3 роки тому +3

    Well explained for the initial level users.

  • @kkartkk9372
    @kkartkk9372 4 роки тому +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  4 роки тому

      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 4 роки тому

      @@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  4 роки тому

      @@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.

  • @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

  • @paulraj6567
    @paulraj6567 Рік тому +1

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

    • @HYRTutorials
      @HYRTutorials  Рік тому

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

  • @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() ?

  • @pankajkore1420
    @pankajkore1420 2 роки тому +1

    Thank you sir..!

  • @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

  • @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

  • @ramanamohan9813
    @ramanamohan9813 2 роки тому +1

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

    • @HYRTutorials
      @HYRTutorials  2 роки тому

      Checkout this video: ua-cam.com/video/deKXbbIHD4w/v-deo.html

  • @-VMamatha
    @-VMamatha 2 роки тому

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

    • @HYRTutorials
      @HYRTutorials  2 роки тому

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

  • @ankitshrivastava1772
    @ankitshrivastava1772 3 роки тому +1

    well explained.

  • @f3century565
    @f3century565 2 роки тому

    i whant to open multiple windows

    • @HYRTutorials
      @HYRTutorials  2 роки тому

      With selenium version 4 you can do that buddy

  • @yeliza2350
    @yeliza2350 2 роки тому +2

    27 February ...it s done🌍

  • @harshinikalyan6457
    @harshinikalyan6457 2 роки тому +1

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

  • @Allinoneclick23
    @Allinoneclick23 2 роки тому

    thank you for making video