How can I handle alert using if else condition in selenium?How do I handle the login modal dialog created by onLoad in a WebDriver & Java based test automationUnderstanding how to handle auto-complete using Selenium and PythonChrome Webdriver click function timeout when opening two simultaneous windowsUnexpected WebDriverWait / FluentWait behaviour - throws Exceptions anywayHow to handle Login pop up by using Selenium, which is used for login to applications?Selenium: getPageSource() returns previous pages' sourceSometimes code is executing perfectly but at other times, the same code is not executingHow can we move to next action immediately after calling webDriver.get() method without waiting for page to change to ready stateDoes implicitIwait affect set_page_load_timeout python, selenium, chrome driverDrag & Drop issue in selenium web driver

Is it improper etiquette to ask your opponent what his/her rating is before the game?

You're three for three

Are MQTT Brokers able to retain/cache some data for a certain amount of time and then send to the subscribers?

Can I rely on these GitHub repository files?

Difference between -| and |- in TikZ

Engineer refusing to file/disclose patents

How to create an folder path alias on ubuntu EC2 instance?

How to open new tab in existing terminal instead of new terminal instance?

JavaScript array of objects contains the same array data

Hostile work environment after whistle-blowing on coworker and our boss. What do I do?

Is it possible to build a CPA Secure encryption scheme which remains secure even when the encryption of secret key is given?

Why did the EU agree to delay the Brexit deadline?

Is there any significance to the Valyrian Stone vault door of Qarth?

Lightning Web Components - Not available in app builder

Proof of Lemma: Every nonzero integer can be written as a product of primes

Could solar power be utilized and substitute coal in the 19th century?

Is it okay / does it make sense for another player to join a running game of Munchkin?

Can somebody explain Brexit in a few child-proof sentences?

General topology proving something for all of its points

Bob has never been a M before

Would it be legal for a US State to ban exports of a natural resource?

Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)

Indicating multiple different modes of speech (fantasy language or telepathy)

Why has "pence" been used in this sentence, not "pences"?



How can I handle alert using if else condition in selenium?


How do I handle the login modal dialog created by onLoad in a WebDriver & Java based test automationUnderstanding how to handle auto-complete using Selenium and PythonChrome Webdriver click function timeout when opening two simultaneous windowsUnexpected WebDriverWait / FluentWait behaviour - throws Exceptions anywayHow to handle Login pop up by using Selenium, which is used for login to applications?Selenium: getPageSource() returns previous pages' sourceSometimes code is executing perfectly but at other times, the same code is not executingHow can we move to next action immediately after calling webDriver.get() method without waiting for page to change to ready stateDoes implicitIwait affect set_page_load_timeout python, selenium, chrome driverDrag & Drop issue in selenium web driver













4















else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("alert was not present");
agobj.logout.click();
else
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");










share|improve this question









New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39















4















else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("alert was not present");
agobj.logout.click();
else
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");










share|improve this question









New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39













4












4








4








else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("alert was not present");
agobj.logout.click();
else
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");










share|improve this question









New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












else is perfectly executed but if block is not executed - can anyone help me?



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10 /*timeout in seconds*/);

if(wait.until(ExpectedConditions.alertIsPresent())==null)
System.out.println("alert was not present");
agobj.logout.click();
else
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");







automated-testing selenium-webdriver selenium2






share|improve this question









New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 20 at 8:31









trashpanda

1,5031929




1,5031929






New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 20 at 6:31









anjith neerukondaanjith neerukonda

242




242




New contributor




anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






anjith neerukonda is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39












  • 2





    Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

    – Michael Durrant
    Mar 20 at 9:39







2




2





Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

– Michael Durrant
Mar 20 at 9:39





Two thoughts - is an alert actually present? - Is waiting until alertISPresent to be null just timing out after 10 seconds?

– Michael Durrant
Mar 20 at 9:39










2 Answers
2






active

oldest

votes


















4














Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

try {
wait.until(ExpectedConditions.alertIsPresent())
Alert alert = Maf_Base.getDriver().switchTo().alert();
alert.accept();
System.out.println("alert was present and accepted");
catch(Exception e)
System.out.println("alert was not present");
System.out.print(e);
agobj.logout.click();






share|improve this answer

























  • "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02











  • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45






  • 1





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47











  • @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50


















3














If you check wait.until() code:



enter image description here



You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






share|improve this answer






















    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "244"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    anjith neerukonda is a new contributor. Be nice, and check out our Code of Conduct.









    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsqa.stackexchange.com%2fquestions%2f38327%2fhow-can-i-handle-alert-using-if-else-condition-in-selenium%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    4














    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e)
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();






    share|improve this answer

























    • "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50















    4














    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e)
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();






    share|improve this answer

























    • "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50













    4












    4








    4







    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e)
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();






    share|improve this answer















    Try a try catch, if something in the try gives a timeout exception (e.g. alert not found) it will execute the code in the catch block.



    WebDriverWait wait = new WebDriverWait(Maf_Base.getDriver(), 10);

    try {
    wait.until(ExpectedConditions.alertIsPresent())
    Alert alert = Maf_Base.getDriver().switchTo().alert();
    alert.accept();
    System.out.println("alert was present and accepted");
    catch(Exception e)
    System.out.println("alert was not present");
    System.out.print(e);
    agobj.logout.click();







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Mar 20 at 10:46

























    answered Mar 20 at 9:47









    Niels van ReijmersdalNiels van Reijmersdal

    21k23172




    21k23172












    • "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50

















    • "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

      – Alexey R.
      Mar 20 at 10:02











    • @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

      – Niels van Reijmersdal
      Mar 20 at 10:45






    • 1





      @AlexeyR. Removed my assumption of how Java works :)

      – Niels van Reijmersdal
      Mar 20 at 10:47











    • @NielsvanReijmersdal thanks, bro!! it's working properly

      – anjith neerukonda
      Mar 20 at 12:50
















    "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02





    "Thus it reads: if(functionObject == null) and a functionObject is not null it is a Object." it is not actually true (if I got you right). ExpectedConditions.alertIsPresent() is "function object" but not wait.until(functionObject). So it does not read if(functionObject == null) but rather if(alert==null) where alert is a product of functionObject.apply() that is called inside until method. However alert cannot be null since until just throw an exception if it would be null. However I agree with general idea of looking up the alert.

    – Alexey R.
    Mar 20 at 10:02













    @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45





    @AlexeyR. I am not a Java expert, but the wait.until returns an alert, but while it is waiting it is not an alert. An IF comparison in most languages do not wait for the return of a function, it just compares the objects in its current state. Still the try/catch will probably solve the issue.

    – Niels van Reijmersdal
    Mar 20 at 10:45




    1




    1





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47





    @AlexeyR. Removed my assumption of how Java works :)

    – Niels van Reijmersdal
    Mar 20 at 10:47













    @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50





    @NielsvanReijmersdal thanks, bro!! it's working properly

    – anjith neerukonda
    Mar 20 at 12:50











    3














    If you check wait.until() code:



    enter image description here



    You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






    share|improve this answer



























      3














      If you check wait.until() code:



      enter image description here



      You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






      share|improve this answer

























        3












        3








        3







        If you check wait.until() code:



        enter image description here



        You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.






        share|improve this answer













        If you check wait.until() code:



        enter image description here



        You will know that it never returns null. It either returns found alert (see lines 88-92) or throws an exception. This is why your if does not work.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 20 at 9:53









        Alexey R.Alexey R.

        8,05511032




        8,05511032




















            anjith neerukonda is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            anjith neerukonda is a new contributor. Be nice, and check out our Code of Conduct.












            anjith neerukonda is a new contributor. Be nice, and check out our Code of Conduct.











            anjith neerukonda is a new contributor. Be nice, and check out our Code of Conduct.














            Thanks for contributing an answer to Software Quality Assurance & Testing Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsqa.stackexchange.com%2fquestions%2f38327%2fhow-can-i-handle-alert-using-if-else-condition-in-selenium%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown