Get.text() in Twitter












1















I'm trying to get the text for the login error message in twitter.



HTML code for this part of Twitter:



<div class="message-inside">
<span class="message-text">The email or password doesn't match.</span>
</div>


I then used:



String message = driver.findElement(By.xpath("/html/body/div[2]/div/div/span")).getText();


Where the xpath is the xpath given by the inspect element of Mozilla. However, the String message is empty.



PS: At the moment getText is called, the message is not hidden, so I don't think that is the problem



EDIT: I have also tried .getAttribute("innerHTML") instead of .getText(), which also failed










share|improve this question

























  • JavaScript != Java. Did you mean to flag this as a JavaScrip question?

    – sofend
    Nov 27 '18 at 21:26











  • @sofend I'm running the Selenium tests in Java through JUnit

    – Marcos Martin
    Nov 27 '18 at 21:29
















1















I'm trying to get the text for the login error message in twitter.



HTML code for this part of Twitter:



<div class="message-inside">
<span class="message-text">The email or password doesn't match.</span>
</div>


I then used:



String message = driver.findElement(By.xpath("/html/body/div[2]/div/div/span")).getText();


Where the xpath is the xpath given by the inspect element of Mozilla. However, the String message is empty.



PS: At the moment getText is called, the message is not hidden, so I don't think that is the problem



EDIT: I have also tried .getAttribute("innerHTML") instead of .getText(), which also failed










share|improve this question

























  • JavaScript != Java. Did you mean to flag this as a JavaScrip question?

    – sofend
    Nov 27 '18 at 21:26











  • @sofend I'm running the Selenium tests in Java through JUnit

    – Marcos Martin
    Nov 27 '18 at 21:29














1












1








1








I'm trying to get the text for the login error message in twitter.



HTML code for this part of Twitter:



<div class="message-inside">
<span class="message-text">The email or password doesn't match.</span>
</div>


I then used:



String message = driver.findElement(By.xpath("/html/body/div[2]/div/div/span")).getText();


Where the xpath is the xpath given by the inspect element of Mozilla. However, the String message is empty.



PS: At the moment getText is called, the message is not hidden, so I don't think that is the problem



EDIT: I have also tried .getAttribute("innerHTML") instead of .getText(), which also failed










share|improve this question
















I'm trying to get the text for the login error message in twitter.



HTML code for this part of Twitter:



<div class="message-inside">
<span class="message-text">The email or password doesn't match.</span>
</div>


I then used:



String message = driver.findElement(By.xpath("/html/body/div[2]/div/div/span")).getText();


Where the xpath is the xpath given by the inspect element of Mozilla. However, the String message is empty.



PS: At the moment getText is called, the message is not hidden, so I don't think that is the problem



EDIT: I have also tried .getAttribute("innerHTML") instead of .getText(), which also failed







java selenium






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 21:10







Marcos Martin

















asked Nov 27 '18 at 21:01









Marcos MartinMarcos Martin

277




277













  • JavaScript != Java. Did you mean to flag this as a JavaScrip question?

    – sofend
    Nov 27 '18 at 21:26











  • @sofend I'm running the Selenium tests in Java through JUnit

    – Marcos Martin
    Nov 27 '18 at 21:29



















  • JavaScript != Java. Did you mean to flag this as a JavaScrip question?

    – sofend
    Nov 27 '18 at 21:26











  • @sofend I'm running the Selenium tests in Java through JUnit

    – Marcos Martin
    Nov 27 '18 at 21:29

















JavaScript != Java. Did you mean to flag this as a JavaScrip question?

– sofend
Nov 27 '18 at 21:26





JavaScript != Java. Did you mean to flag this as a JavaScrip question?

– sofend
Nov 27 '18 at 21:26













@sofend I'm running the Selenium tests in Java through JUnit

– Marcos Martin
Nov 27 '18 at 21:29





@sofend I'm running the Selenium tests in Java through JUnit

– Marcos Martin
Nov 27 '18 at 21:29












3 Answers
3






active

oldest

votes


















2














Never resolve such issues with sleep: it's unreliable (what if element appeared 1ms later than sleep length?) and slows your tests (what if element appeared 10ms after sleep start? you wasted 990ms, and on many tests it adds up).



Instead, use explicit wait pattern to wait for text to appear:



WebDriverWait wait = new WebDriverWait(driver, 10); // wait for max 10 sec
String message = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/span")).getText();


This way you tell selenium to wait for element for up to 10 seconds, but it will return faster if element appears before that.



Another thing: xpath like /html/body/div[2]/div/div/span is not something you should normally use (there are situations when it's inevitable, but rarely). Why:




  • First of all from looking at the path, you can't tell what is it that you are choosing. So if HTML changes, you will have a hard time figuring out what this statement did before.

  • Selection by index (e.g. div[2]) is very breakable: all it takes is for one more div to appear above the one you selected, and your xpath will break

  • You rarely need to select absolute path. Concentrate on parts that matter.


For instance what you are trying to select, looks like this on Twitter:



 <div class="alert-messages" id="message-drawer" style="top: -40px;">
<div class="message ">
<div class="message-inside">
<span class="message-text"></span>
</div>
</div>
</div>


So a more meaningful xpath would be



//div[@id='message-drawer']//span[@class='message-text']


which tells you which part of the page you are looking at - message-drawer (and id is best to identify things - it's always unique). And which element inside that part you are interested in. This is it. If anything on page changes, you mostly won't care. And even if you do, xpath clearly tells you the purpose of your selection, so easier to understand what it meant.






share|improve this answer


























  • I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

    – Marcos Martin
    Nov 28 '18 at 23:02



















1














Try changing the path to div[1] or removing one of the "/div"'s in the path.






share|improve this answer
























  • I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

    – Zach Pedigo
    Nov 27 '18 at 21:05











  • No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

    – Marcos Martin
    Nov 27 '18 at 21:08











  • So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

    – Zach Pedigo
    Nov 27 '18 at 21:21











  • Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

    – Marcos Martin
    Nov 27 '18 at 21:25











  • If I may, what is the purpose behind grabbing the error message text?

    – Zach Pedigo
    Nov 27 '18 at 22:48



















1














Ok, so apparently the error message WAS hidden when I tried to get the text. I told Selenium to wait a little before getting the text (and after inputing a wrong login) with:



Thread.sleep(1000);


and it was able to get the text without troubles. If you're trying to get a text that was previously hidden, tell Selenium to wait a little, so you can make sure it is no longer hidden.






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%2f53508084%2fget-text-in-twitter%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









    2














    Never resolve such issues with sleep: it's unreliable (what if element appeared 1ms later than sleep length?) and slows your tests (what if element appeared 10ms after sleep start? you wasted 990ms, and on many tests it adds up).



    Instead, use explicit wait pattern to wait for text to appear:



    WebDriverWait wait = new WebDriverWait(driver, 10); // wait for max 10 sec
    String message = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/span")).getText();


    This way you tell selenium to wait for element for up to 10 seconds, but it will return faster if element appears before that.



    Another thing: xpath like /html/body/div[2]/div/div/span is not something you should normally use (there are situations when it's inevitable, but rarely). Why:




    • First of all from looking at the path, you can't tell what is it that you are choosing. So if HTML changes, you will have a hard time figuring out what this statement did before.

    • Selection by index (e.g. div[2]) is very breakable: all it takes is for one more div to appear above the one you selected, and your xpath will break

    • You rarely need to select absolute path. Concentrate on parts that matter.


    For instance what you are trying to select, looks like this on Twitter:



     <div class="alert-messages" id="message-drawer" style="top: -40px;">
    <div class="message ">
    <div class="message-inside">
    <span class="message-text"></span>
    </div>
    </div>
    </div>


    So a more meaningful xpath would be



    //div[@id='message-drawer']//span[@class='message-text']


    which tells you which part of the page you are looking at - message-drawer (and id is best to identify things - it's always unique). And which element inside that part you are interested in. This is it. If anything on page changes, you mostly won't care. And even if you do, xpath clearly tells you the purpose of your selection, so easier to understand what it meant.






    share|improve this answer


























    • I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

      – Marcos Martin
      Nov 28 '18 at 23:02
















    2














    Never resolve such issues with sleep: it's unreliable (what if element appeared 1ms later than sleep length?) and slows your tests (what if element appeared 10ms after sleep start? you wasted 990ms, and on many tests it adds up).



    Instead, use explicit wait pattern to wait for text to appear:



    WebDriverWait wait = new WebDriverWait(driver, 10); // wait for max 10 sec
    String message = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/span")).getText();


    This way you tell selenium to wait for element for up to 10 seconds, but it will return faster if element appears before that.



    Another thing: xpath like /html/body/div[2]/div/div/span is not something you should normally use (there are situations when it's inevitable, but rarely). Why:




    • First of all from looking at the path, you can't tell what is it that you are choosing. So if HTML changes, you will have a hard time figuring out what this statement did before.

    • Selection by index (e.g. div[2]) is very breakable: all it takes is for one more div to appear above the one you selected, and your xpath will break

    • You rarely need to select absolute path. Concentrate on parts that matter.


    For instance what you are trying to select, looks like this on Twitter:



     <div class="alert-messages" id="message-drawer" style="top: -40px;">
    <div class="message ">
    <div class="message-inside">
    <span class="message-text"></span>
    </div>
    </div>
    </div>


    So a more meaningful xpath would be



    //div[@id='message-drawer']//span[@class='message-text']


    which tells you which part of the page you are looking at - message-drawer (and id is best to identify things - it's always unique). And which element inside that part you are interested in. This is it. If anything on page changes, you mostly won't care. And even if you do, xpath clearly tells you the purpose of your selection, so easier to understand what it meant.






    share|improve this answer


























    • I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

      – Marcos Martin
      Nov 28 '18 at 23:02














    2












    2








    2







    Never resolve such issues with sleep: it's unreliable (what if element appeared 1ms later than sleep length?) and slows your tests (what if element appeared 10ms after sleep start? you wasted 990ms, and on many tests it adds up).



    Instead, use explicit wait pattern to wait for text to appear:



    WebDriverWait wait = new WebDriverWait(driver, 10); // wait for max 10 sec
    String message = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/span")).getText();


    This way you tell selenium to wait for element for up to 10 seconds, but it will return faster if element appears before that.



    Another thing: xpath like /html/body/div[2]/div/div/span is not something you should normally use (there are situations when it's inevitable, but rarely). Why:




    • First of all from looking at the path, you can't tell what is it that you are choosing. So if HTML changes, you will have a hard time figuring out what this statement did before.

    • Selection by index (e.g. div[2]) is very breakable: all it takes is for one more div to appear above the one you selected, and your xpath will break

    • You rarely need to select absolute path. Concentrate on parts that matter.


    For instance what you are trying to select, looks like this on Twitter:



     <div class="alert-messages" id="message-drawer" style="top: -40px;">
    <div class="message ">
    <div class="message-inside">
    <span class="message-text"></span>
    </div>
    </div>
    </div>


    So a more meaningful xpath would be



    //div[@id='message-drawer']//span[@class='message-text']


    which tells you which part of the page you are looking at - message-drawer (and id is best to identify things - it's always unique). And which element inside that part you are interested in. This is it. If anything on page changes, you mostly won't care. And even if you do, xpath clearly tells you the purpose of your selection, so easier to understand what it meant.






    share|improve this answer















    Never resolve such issues with sleep: it's unreliable (what if element appeared 1ms later than sleep length?) and slows your tests (what if element appeared 10ms after sleep start? you wasted 990ms, and on many tests it adds up).



    Instead, use explicit wait pattern to wait for text to appear:



    WebDriverWait wait = new WebDriverWait(driver, 10); // wait for max 10 sec
    String message = wait.until(ExpectedConditions. visibilityOfElementLocated(By.xpath("/html/body/div[2]/div/div/span")).getText();


    This way you tell selenium to wait for element for up to 10 seconds, but it will return faster if element appears before that.



    Another thing: xpath like /html/body/div[2]/div/div/span is not something you should normally use (there are situations when it's inevitable, but rarely). Why:




    • First of all from looking at the path, you can't tell what is it that you are choosing. So if HTML changes, you will have a hard time figuring out what this statement did before.

    • Selection by index (e.g. div[2]) is very breakable: all it takes is for one more div to appear above the one you selected, and your xpath will break

    • You rarely need to select absolute path. Concentrate on parts that matter.


    For instance what you are trying to select, looks like this on Twitter:



     <div class="alert-messages" id="message-drawer" style="top: -40px;">
    <div class="message ">
    <div class="message-inside">
    <span class="message-text"></span>
    </div>
    </div>
    </div>


    So a more meaningful xpath would be



    //div[@id='message-drawer']//span[@class='message-text']


    which tells you which part of the page you are looking at - message-drawer (and id is best to identify things - it's always unique). And which element inside that part you are interested in. This is it. If anything on page changes, you mostly won't care. And even if you do, xpath clearly tells you the purpose of your selection, so easier to understand what it meant.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '18 at 0:42

























    answered Nov 27 '18 at 22:48









    Kiril S.Kiril S.

    5,71152145




    5,71152145













    • I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

      – Marcos Martin
      Nov 28 '18 at 23:02



















    • I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

      – Marcos Martin
      Nov 28 '18 at 23:02

















    I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

    – Marcos Martin
    Nov 28 '18 at 23:02





    I don't know if Twitter used Ids, I've just seen one time. But I get what you said, I'll try making it more generic. Thanks!

    – Marcos Martin
    Nov 28 '18 at 23:02













    1














    Try changing the path to div[1] or removing one of the "/div"'s in the path.






    share|improve this answer
























    • I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

      – Zach Pedigo
      Nov 27 '18 at 21:05











    • No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

      – Marcos Martin
      Nov 27 '18 at 21:08











    • So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

      – Zach Pedigo
      Nov 27 '18 at 21:21











    • Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

      – Marcos Martin
      Nov 27 '18 at 21:25











    • If I may, what is the purpose behind grabbing the error message text?

      – Zach Pedigo
      Nov 27 '18 at 22:48
















    1














    Try changing the path to div[1] or removing one of the "/div"'s in the path.






    share|improve this answer
























    • I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

      – Zach Pedigo
      Nov 27 '18 at 21:05











    • No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

      – Marcos Martin
      Nov 27 '18 at 21:08











    • So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

      – Zach Pedigo
      Nov 27 '18 at 21:21











    • Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

      – Marcos Martin
      Nov 27 '18 at 21:25











    • If I may, what is the purpose behind grabbing the error message text?

      – Zach Pedigo
      Nov 27 '18 at 22:48














    1












    1








    1







    Try changing the path to div[1] or removing one of the "/div"'s in the path.






    share|improve this answer













    Try changing the path to div[1] or removing one of the "/div"'s in the path.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 27 '18 at 21:04









    Zach PedigoZach Pedigo

    776




    776













    • I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

      – Zach Pedigo
      Nov 27 '18 at 21:05











    • No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

      – Marcos Martin
      Nov 27 '18 at 21:08











    • So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

      – Zach Pedigo
      Nov 27 '18 at 21:21











    • Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

      – Marcos Martin
      Nov 27 '18 at 21:25











    • If I may, what is the purpose behind grabbing the error message text?

      – Zach Pedigo
      Nov 27 '18 at 22:48



















    • I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

      – Zach Pedigo
      Nov 27 '18 at 21:05











    • No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

      – Marcos Martin
      Nov 27 '18 at 21:08











    • So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

      – Zach Pedigo
      Nov 27 '18 at 21:21











    • Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

      – Marcos Martin
      Nov 27 '18 at 21:25











    • If I may, what is the purpose behind grabbing the error message text?

      – Zach Pedigo
      Nov 27 '18 at 22:48

















    I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

    – Zach Pedigo
    Nov 27 '18 at 21:05





    I say just change the path because if it's not retrieving the text then its probably not getting the correct path.

    – Zach Pedigo
    Nov 27 '18 at 21:05













    No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

    – Marcos Martin
    Nov 27 '18 at 21:08





    No deal. NoSuchElement exception. I have also tried using the cssSelector instead of xpath, which also failed.

    – Marcos Martin
    Nov 27 '18 at 21:08













    So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

    – Zach Pedigo
    Nov 27 '18 at 21:21





    So if this is the correct path to the field you are looking for, my next question is...is this trying to get the text of the username or password field?

    – Zach Pedigo
    Nov 27 '18 at 21:21













    Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

    – Marcos Martin
    Nov 27 '18 at 21:25





    Nope, it is trying to get the text of the error message when you type a wrong login. I cannot vouche for this being the correct path, but I've tried using different locators.

    – Marcos Martin
    Nov 27 '18 at 21:25













    If I may, what is the purpose behind grabbing the error message text?

    – Zach Pedigo
    Nov 27 '18 at 22:48





    If I may, what is the purpose behind grabbing the error message text?

    – Zach Pedigo
    Nov 27 '18 at 22:48











    1














    Ok, so apparently the error message WAS hidden when I tried to get the text. I told Selenium to wait a little before getting the text (and after inputing a wrong login) with:



    Thread.sleep(1000);


    and it was able to get the text without troubles. If you're trying to get a text that was previously hidden, tell Selenium to wait a little, so you can make sure it is no longer hidden.






    share|improve this answer




























      1














      Ok, so apparently the error message WAS hidden when I tried to get the text. I told Selenium to wait a little before getting the text (and after inputing a wrong login) with:



      Thread.sleep(1000);


      and it was able to get the text without troubles. If you're trying to get a text that was previously hidden, tell Selenium to wait a little, so you can make sure it is no longer hidden.






      share|improve this answer


























        1












        1








        1







        Ok, so apparently the error message WAS hidden when I tried to get the text. I told Selenium to wait a little before getting the text (and after inputing a wrong login) with:



        Thread.sleep(1000);


        and it was able to get the text without troubles. If you're trying to get a text that was previously hidden, tell Selenium to wait a little, so you can make sure it is no longer hidden.






        share|improve this answer













        Ok, so apparently the error message WAS hidden when I tried to get the text. I told Selenium to wait a little before getting the text (and after inputing a wrong login) with:



        Thread.sleep(1000);


        and it was able to get the text without troubles. If you're trying to get a text that was previously hidden, tell Selenium to wait a little, so you can make sure it is no longer hidden.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 21:41









        Marcos MartinMarcos Martin

        277




        277






























            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%2f53508084%2fget-text-in-twitter%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

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks