check field is present or not in selenium javaWatiN or Selenium?Typing Enter/Return key in SeleniumScroll Element into View with SeleniumHow to take screenshot with Selenium WebDriverWait for page load in SeleniumSelenium c# Webdriver: Wait Until Element is PresentGet HTML Source of WebElement in Selenium WebDriver using PythonTest if element is present using Selenium WebDriver?How to use Webelement List in Selenium webdriver for the given scenario?Selenium webdriver java wait for element present

Why does Intel's Haswell chip allow multiplication to be twice as fast as addition?

Why isn’t SHA-3 in wider use?

How can I tell if a flight itinerary is fake?

What does "sardine box" mean?

Want to draw this commutative diagram

During the Space Shuttle Columbia Disaster of 2003, Why Did The Flight Director Say, "Lock the doors."?

How do Mogwai reproduce?

sed delete all the words before a match

What word can be used to describe a bug in a movie?

A stranger from Norway wants to have money delivered to me

Shabbat clothing on shabbat chazon

Does a code snippet compile? Or does it get compiled?

Was the 2019 Lion King film made through motion capture?

In a topological space if there exists a loop that cannot be contracted to a point does there exist a simple loop that cannot be contracted also?

How many hit points does the Battle Smith Artificer's Iron Defender have?

How do I calculate the difference in lens reach between a superzoom compact and a DSLR zoom lens?

Why did Gandalf use a sword against the Balrog?

Can I call myself an assistant professor without a PhD?

Is it really ~648.69 km/s delta-v to "land" on the surface of the Sun?

Why "ch" pronunciation rule doesn't occur for words such as "durch", "manchmal"?

How to mark beverage cans in a cooler for a blind person?

Infeasibility in mathematical optimization models

As a 16 year old, how can I keep my money safe from my mother?

Are any jet engines used in combat aircraft water cooled?



check field is present or not in selenium java


WatiN or Selenium?Typing Enter/Return key in SeleniumScroll Element into View with SeleniumHow to take screenshot with Selenium WebDriverWait for page load in SeleniumSelenium c# Webdriver: Wait Until Element is PresentGet HTML Source of WebElement in Selenium WebDriver using PythonTest if element is present using Selenium WebDriver?How to use Webelement List in Selenium webdriver for the given scenario?Selenium webdriver java wait for element present






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















I need to find if an element is displayed or not. How to check that in selenium web driver?



if(driver.findElement(By.id("p_first_name")).isDisplayed()) 



WebElement fname =driver.findElement(By.id("p_first_name"));
fname.sendKeys("pradnya");

WebElement lname = driver.findElement(By.xpath("//*[@id="p_last_name"]"));
lname.sendKeys("Bolli");

WebElement Address1 = driver.findElement(By.xpath("//*[@id="address_11"]"));
Address1.sendKeys("New address1");

WebElement Address2 = driver.findElement(By.xpath("//*[@id="address_21"]"));
Address2.sendKeys("New address2");

WebElement City = driver.findElement(By.xpath("//*[@id="city1"]"));
City.sendKeys("Pune");

WebElement Country = driver.findElement(By.xpath("//*[@id="country1"]"));
Country.sendKeys("India");

WebElement ZipCode = driver.findElement(By.xpath("//*[@id="pincode1"]"));
ZipCode.sendKeys("India");

WebElement State = driver.findElement(By.xpath("//*[@id="bds"]"));
State.sendKeys("Maharashtra");


else

WebElement address = driver.findElement(By.xpath("//*[@id="update_add77"]"));
address.click();



On the checkout page first it shows address form and when user fils that then listing is shown. Address form is not showing when the listing is showing. In this case, how to check if an address form field is displayed or not?



I use above code but it gives me exception message



 'Unable to locate element: #p_first_name'









share|improve this question


























  • I used try catch then code run successfully

    – Pradnya Bolli
    Mar 27 at 12:48

















0















I need to find if an element is displayed or not. How to check that in selenium web driver?



if(driver.findElement(By.id("p_first_name")).isDisplayed()) 



WebElement fname =driver.findElement(By.id("p_first_name"));
fname.sendKeys("pradnya");

WebElement lname = driver.findElement(By.xpath("//*[@id="p_last_name"]"));
lname.sendKeys("Bolli");

WebElement Address1 = driver.findElement(By.xpath("//*[@id="address_11"]"));
Address1.sendKeys("New address1");

WebElement Address2 = driver.findElement(By.xpath("//*[@id="address_21"]"));
Address2.sendKeys("New address2");

WebElement City = driver.findElement(By.xpath("//*[@id="city1"]"));
City.sendKeys("Pune");

WebElement Country = driver.findElement(By.xpath("//*[@id="country1"]"));
Country.sendKeys("India");

WebElement ZipCode = driver.findElement(By.xpath("//*[@id="pincode1"]"));
ZipCode.sendKeys("India");

WebElement State = driver.findElement(By.xpath("//*[@id="bds"]"));
State.sendKeys("Maharashtra");


else

WebElement address = driver.findElement(By.xpath("//*[@id="update_add77"]"));
address.click();



On the checkout page first it shows address form and when user fils that then listing is shown. Address form is not showing when the listing is showing. In this case, how to check if an address form field is displayed or not?



I use above code but it gives me exception message



 'Unable to locate element: #p_first_name'









share|improve this question


























  • I used try catch then code run successfully

    – Pradnya Bolli
    Mar 27 at 12:48













0












0








0








I need to find if an element is displayed or not. How to check that in selenium web driver?



if(driver.findElement(By.id("p_first_name")).isDisplayed()) 



WebElement fname =driver.findElement(By.id("p_first_name"));
fname.sendKeys("pradnya");

WebElement lname = driver.findElement(By.xpath("//*[@id="p_last_name"]"));
lname.sendKeys("Bolli");

WebElement Address1 = driver.findElement(By.xpath("//*[@id="address_11"]"));
Address1.sendKeys("New address1");

WebElement Address2 = driver.findElement(By.xpath("//*[@id="address_21"]"));
Address2.sendKeys("New address2");

WebElement City = driver.findElement(By.xpath("//*[@id="city1"]"));
City.sendKeys("Pune");

WebElement Country = driver.findElement(By.xpath("//*[@id="country1"]"));
Country.sendKeys("India");

WebElement ZipCode = driver.findElement(By.xpath("//*[@id="pincode1"]"));
ZipCode.sendKeys("India");

WebElement State = driver.findElement(By.xpath("//*[@id="bds"]"));
State.sendKeys("Maharashtra");


else

WebElement address = driver.findElement(By.xpath("//*[@id="update_add77"]"));
address.click();



On the checkout page first it shows address form and when user fils that then listing is shown. Address form is not showing when the listing is showing. In this case, how to check if an address form field is displayed or not?



I use above code but it gives me exception message



 'Unable to locate element: #p_first_name'









share|improve this question
















I need to find if an element is displayed or not. How to check that in selenium web driver?



if(driver.findElement(By.id("p_first_name")).isDisplayed()) 



WebElement fname =driver.findElement(By.id("p_first_name"));
fname.sendKeys("pradnya");

WebElement lname = driver.findElement(By.xpath("//*[@id="p_last_name"]"));
lname.sendKeys("Bolli");

WebElement Address1 = driver.findElement(By.xpath("//*[@id="address_11"]"));
Address1.sendKeys("New address1");

WebElement Address2 = driver.findElement(By.xpath("//*[@id="address_21"]"));
Address2.sendKeys("New address2");

WebElement City = driver.findElement(By.xpath("//*[@id="city1"]"));
City.sendKeys("Pune");

WebElement Country = driver.findElement(By.xpath("//*[@id="country1"]"));
Country.sendKeys("India");

WebElement ZipCode = driver.findElement(By.xpath("//*[@id="pincode1"]"));
ZipCode.sendKeys("India");

WebElement State = driver.findElement(By.xpath("//*[@id="bds"]"));
State.sendKeys("Maharashtra");


else

WebElement address = driver.findElement(By.xpath("//*[@id="update_add77"]"));
address.click();



On the checkout page first it shows address form and when user fils that then listing is shown. Address form is not showing when the listing is showing. In this case, how to check if an address form field is displayed or not?



I use above code but it gives me exception message



 'Unable to locate element: #p_first_name'






selenium selenium-webdriver automated-tests browser-automation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 at 7:48









Mate Mrše

2,7503 gold badges11 silver badges33 bronze badges




2,7503 gold badges11 silver badges33 bronze badges










asked Mar 27 at 7:38









Pradnya BolliPradnya Bolli

1,47511 silver badges26 bronze badges




1,47511 silver badges26 bronze badges















  • I used try catch then code run successfully

    – Pradnya Bolli
    Mar 27 at 12:48

















  • I used try catch then code run successfully

    – Pradnya Bolli
    Mar 27 at 12:48
















I used try catch then code run successfully

– Pradnya Bolli
Mar 27 at 12:48





I used try catch then code run successfully

– Pradnya Bolli
Mar 27 at 12:48












3 Answers
3






active

oldest

votes


















3














The element is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it using the isDisplayed() method.



So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.

You need to make the following changes in the code:



if(driver.findElements(By.id("p_first_name")).size()>0)
// Add the if code here

else
// Add the else code here






share|improve this answer
































    0














    You can create method for such check. We are using NoSuchElementException to verify element is not existing.



    public boolean isElementExist(By locator)

    try
    driver.findElement(locator);
    catch (NoSuchElementException e)
    return false;


    return true;



    Or due slow loading and timeouts, i advice to use *WebDriverWait*






    share|improve this answer
































      0














      public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) 
      try
      WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
      wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
      wait.ignoring(NoSuchElementException.class);
      wait.ignoring(ElementNotVisibleException.class);
      wait.ignoring(StaleElementReferenceException.class);
      wait.ignoring(NoSuchFrameException.class);
      wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
      return true;

      catch(Exception e)
      return false;





      If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms






      share|improve this answer



























        Your Answer






        StackExchange.ifUsing("editor", function ()
        StackExchange.using("externalEditor", function ()
        StackExchange.using("snippets", function ()
        StackExchange.snippets.init();
        );
        );
        , "code-snippets");

        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "1"
        ;
        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: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        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
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55371999%2fcheck-field-is-present-or-not-in-selenium-java%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        The element is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it using the isDisplayed() method.



        So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.

        You need to make the following changes in the code:



        if(driver.findElements(By.id("p_first_name")).size()>0)
        // Add the if code here

        else
        // Add the else code here






        share|improve this answer





























          3














          The element is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it using the isDisplayed() method.



          So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.

          You need to make the following changes in the code:



          if(driver.findElements(By.id("p_first_name")).size()>0)
          // Add the if code here

          else
          // Add the else code here






          share|improve this answer



























            3












            3








            3







            The element is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it using the isDisplayed() method.



            So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.

            You need to make the following changes in the code:



            if(driver.findElements(By.id("p_first_name")).size()>0)
            // Add the if code here

            else
            // Add the else code here






            share|improve this answer













            The element is giving NoSuchElementException as the element is not present on the UI on which you are trying to find it using the isDisplayed() method.



            So, to solve your problem you should fetch the list of the element and then can get the size of that list, if the size is greater than 0, it means the element is present on the page else the element is not present.

            You need to make the following changes in the code:



            if(driver.findElements(By.id("p_first_name")).size()>0)
            // Add the if code here

            else
            // Add the else code here







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 7:46









            Sameer AroraSameer Arora

            2,3482 gold badges3 silver badges15 bronze badges




            2,3482 gold badges3 silver badges15 bronze badges


























                0














                You can create method for such check. We are using NoSuchElementException to verify element is not existing.



                public boolean isElementExist(By locator)

                try
                driver.findElement(locator);
                catch (NoSuchElementException e)
                return false;


                return true;



                Or due slow loading and timeouts, i advice to use *WebDriverWait*






                share|improve this answer





























                  0














                  You can create method for such check. We are using NoSuchElementException to verify element is not existing.



                  public boolean isElementExist(By locator)

                  try
                  driver.findElement(locator);
                  catch (NoSuchElementException e)
                  return false;


                  return true;



                  Or due slow loading and timeouts, i advice to use *WebDriverWait*






                  share|improve this answer



























                    0












                    0








                    0







                    You can create method for such check. We are using NoSuchElementException to verify element is not existing.



                    public boolean isElementExist(By locator)

                    try
                    driver.findElement(locator);
                    catch (NoSuchElementException e)
                    return false;


                    return true;



                    Or due slow loading and timeouts, i advice to use *WebDriverWait*






                    share|improve this answer













                    You can create method for such check. We are using NoSuchElementException to verify element is not existing.



                    public boolean isElementExist(By locator)

                    try
                    driver.findElement(locator);
                    catch (NoSuchElementException e)
                    return false;


                    return true;



                    Or due slow loading and timeouts, i advice to use *WebDriverWait*







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Mar 27 at 7:57









                    Infern0Infern0

                    1,3911 gold badge3 silver badges14 bronze badges




                    1,3911 gold badge3 silver badges14 bronze badges
























                        0














                        public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) 
                        try
                        WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
                        wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
                        wait.ignoring(NoSuchElementException.class);
                        wait.ignoring(ElementNotVisibleException.class);
                        wait.ignoring(StaleElementReferenceException.class);
                        wait.ignoring(NoSuchFrameException.class);
                        wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
                        return true;

                        catch(Exception e)
                        return false;





                        If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms






                        share|improve this answer





























                          0














                          public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) 
                          try
                          WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
                          wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
                          wait.ignoring(NoSuchElementException.class);
                          wait.ignoring(ElementNotVisibleException.class);
                          wait.ignoring(StaleElementReferenceException.class);
                          wait.ignoring(NoSuchFrameException.class);
                          wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
                          return true;

                          catch(Exception e)
                          return false;





                          If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms






                          share|improve this answer



























                            0












                            0








                            0







                            public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) 
                            try
                            WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
                            wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
                            wait.ignoring(NoSuchElementException.class);
                            wait.ignoring(ElementNotVisibleException.class);
                            wait.ignoring(StaleElementReferenceException.class);
                            wait.ignoring(NoSuchFrameException.class);
                            wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
                            return true;

                            catch(Exception e)
                            return false;





                            If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms






                            share|improve this answer













                            public boolean isElementPresent(By element,int timeOutInSeconds,int pollingEveryInMiliSec) 
                            try
                            WebDriverWait wait = new WebDriverWait(d, timeOutInSeconds);
                            wait.pollingEvery(pollingEveryInMiliSec, TimeUnit.MILLISECONDS);
                            wait.ignoring(NoSuchElementException.class);
                            wait.ignoring(ElementNotVisibleException.class);
                            wait.ignoring(StaleElementReferenceException.class);
                            wait.ignoring(NoSuchFrameException.class);
                            wait.until(ExpectedConditions.visibilityOfElementLocated(element) ));
                            return true;

                            catch(Exception e)
                            return false;





                            If you consider timeOutInSeconds=20 and pollingEveryInMiliSec=5 in every 5 ms this method will search for the giving element until it find's it with in 20ms







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Mar 27 at 10:30









                            sree rajasree raja

                            1326 bronze badges




                            1326 bronze badges






























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Stack Overflow!


                                • 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%2fstackoverflow.com%2fquestions%2f55371999%2fcheck-field-is-present-or-not-in-selenium-java%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







                                Popular posts from this blog

                                Obelisk of Theodosius Contents History Description Notes Bibliography Further reading External links Navigation menuAge of spirituality : late antique and early Christian art, third to seventh centuryOver 60 picturesObelisks of the World41°00′21.24″N 28°58′31.43″E / 41.0059000°N 28.9753972°E / 41.0059000; 28.97539727724550-7235741376235741376

                                밀양 대씨 역사 각주 함께 보기 둘러보기 메뉴밀양 대씨

                                1973년 목차 사건 문화 탄생 사망 노벨상 달력 둘러보기 메뉴