Selenium Webdriver: is it a professional practice to use xpath?











up vote
2
down vote

favorite












I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.



I tried: className, name, cssSelector e.t.c. with e.g.



driver.findElement(By.className("kujes")).click();


This is what worked.



driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?










share|improve this question






















  • From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
    – AJC24
    Nov 10 at 22:25












  • Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
    – pasignature
    Nov 10 at 22:46










  • Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
    – AJC24
    Nov 10 at 23:24















up vote
2
down vote

favorite












I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.



I tried: className, name, cssSelector e.t.c. with e.g.



driver.findElement(By.className("kujes")).click();


This is what worked.



driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?










share|improve this question






















  • From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
    – AJC24
    Nov 10 at 22:25












  • Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
    – pasignature
    Nov 10 at 22:46










  • Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
    – AJC24
    Nov 10 at 23:24













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.



I tried: className, name, cssSelector e.t.c. with e.g.



driver.findElement(By.className("kujes")).click();


This is what worked.



driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?










share|improve this question













I am testing a web app using selenium and java. I've always avoided xpath like it was a disease. Unfortunately, I got stuck on a stubborn web element buried deep inside a table unfortunately with no id or class. I tried everything and even invited my great great grand parents but nay...nothing worked, except xpath...see below.



I tried: className, name, cssSelector e.t.c. with e.g.



driver.findElement(By.className("kujes")).click();


This is what worked.



driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


I do not want anything less than professional in my work.
So, my questions are is xpath reliable and a good practice?
Is it professional to use xpath?







java selenium






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 22:21









pasignature

104




104












  • From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
    – AJC24
    Nov 10 at 22:25












  • Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
    – pasignature
    Nov 10 at 22:46










  • Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
    – AJC24
    Nov 10 at 23:24


















  • From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
    – AJC24
    Nov 10 at 22:25












  • Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
    – pasignature
    Nov 10 at 22:46










  • Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
    – AJC24
    Nov 10 at 23:24
















From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25






From my experience it depends on the company you work for as regards how professional either of these are to use. One place I worked said "absolutely no XPaths" as they found them unreliable cross browser / device. They insisted on CSS selectors. Another company would only use XPaths. Personally, having used both, I prefer using CSS selectors. Simple. Clean. Gets the job done and has been 100% reliable on any browser / device I've used them with.
– AJC24
Nov 10 at 22:25














Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46




Thanks AJC24 but I am wondering....can css selectors work even when there is no id or classname? because I gave it a shot too and it failed.
– pasignature
Nov 10 at 22:46












Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
– AJC24
Nov 10 at 23:24




Of course they can but it helps massively if your elements have id or class properties. If you're the developer - perhaps you should consider adding them. If you're not, you should request that they're added. You can do things like, for example, div[id="parent"] > div > p > h2 to traverse down through the DOM elements to the selector you want using CSS selectors.
– AJC24
Nov 10 at 23:24












2 Answers
2






active

oldest

votes

















up vote
3
down vote













driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


The above approach is very very bad practice.



Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.






share|improve this answer






























    up vote
    2
    down vote













    It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.



    First you can try with id or class name which are unique.



    Then we can play with css selector to find,




    • Element with attribute, classname , id and combination.

    • Element which is child of another element,

    • Element which is next sibling of another element.


    You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.



    driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()


    You can use relative xpath



    driver.findElement(By.xpath("//table[@id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()


    There are few cases which are possible only with XPath in selenium




    • Finding parent element of an element

    • Finding preceding sibling of an element

    • Finding an element with innerText

    • Finding nth element of the locator


    The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.






    share|improve this answer























    • "[...] find selector which is [...] never changing" - dream on! :)
      – Andrejs
      Nov 11 at 19:16






    • 1




      Ha ha ! Updated the sentence
      – Navarasu
      Nov 11 at 19:53











    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',
    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%2f53244005%2fselenium-webdriver-is-it-a-professional-practice-to-use-xpath%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








    up vote
    3
    down vote













    driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


    The above approach is very very bad practice.



    Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.






    share|improve this answer



























      up vote
      3
      down vote













      driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


      The above approach is very very bad practice.



      Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.






      share|improve this answer

























        up vote
        3
        down vote










        up vote
        3
        down vote









        driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


        The above approach is very very bad practice.



        Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.






        share|improve this answer














        driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click();


        The above approach is very very bad practice.



        Never use indexes in your xpath. It becomes very fragile and will break every single time even when there is a small change in the target application. Try to ask the developers to add ID to that object.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 16:57









        Navarasu

        1,7981721




        1,7981721










        answered Nov 10 at 22:30









        Damien-Amen

        1,38762143




        1,38762143
























            up vote
            2
            down vote













            It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.



            First you can try with id or class name which are unique.



            Then we can play with css selector to find,




            • Element with attribute, classname , id and combination.

            • Element which is child of another element,

            • Element which is next sibling of another element.


            You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.



            driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()


            You can use relative xpath



            driver.findElement(By.xpath("//table[@id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()


            There are few cases which are possible only with XPath in selenium




            • Finding parent element of an element

            • Finding preceding sibling of an element

            • Finding an element with innerText

            • Finding nth element of the locator


            The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.






            share|improve this answer























            • "[...] find selector which is [...] never changing" - dream on! :)
              – Andrejs
              Nov 11 at 19:16






            • 1




              Ha ha ! Updated the sentence
              – Navarasu
              Nov 11 at 19:53















            up vote
            2
            down vote













            It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.



            First you can try with id or class name which are unique.



            Then we can play with css selector to find,




            • Element with attribute, classname , id and combination.

            • Element which is child of another element,

            • Element which is next sibling of another element.


            You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.



            driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()


            You can use relative xpath



            driver.findElement(By.xpath("//table[@id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()


            There are few cases which are possible only with XPath in selenium




            • Finding parent element of an element

            • Finding preceding sibling of an element

            • Finding an element with innerText

            • Finding nth element of the locator


            The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.






            share|improve this answer























            • "[...] find selector which is [...] never changing" - dream on! :)
              – Andrejs
              Nov 11 at 19:16






            • 1




              Ha ha ! Updated the sentence
              – Navarasu
              Nov 11 at 19:53













            up vote
            2
            down vote










            up vote
            2
            down vote









            It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.



            First you can try with id or class name which are unique.



            Then we can play with css selector to find,




            • Element with attribute, classname , id and combination.

            • Element which is child of another element,

            • Element which is next sibling of another element.


            You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.



            driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()


            You can use relative xpath



            driver.findElement(By.xpath("//table[@id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()


            There are few cases which are possible only with XPath in selenium




            • Finding parent element of an element

            • Finding preceding sibling of an element

            • Finding an element with innerText

            • Finding nth element of the locator


            The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.






            share|improve this answer














            It depends on the cases. Ultimate goal is to find selector which is unique and never changing until big change happens.



            First you can try with id or class name which are unique.



            Then we can play with css selector to find,




            • Element with attribute, classname , id and combination.

            • Element which is child of another element,

            • Element which is next sibling of another element.


            You are using absolute xpath, which is unreadable and changing one. Using absolute xpath is completely unprofessional.



            driver.findElement(By.xpath("/html/body/div[7]/div[3]/div/div[2]/div[1]/div[2]/div/div/div/div/div[2]/div/div[1]/div/div/div[6]/div/div[2]/div[2]/div/table/tbody/tr[1]/td[3]")).click()


            You can use relative xpath



            driver.findElement(By.xpath("//table[@id='somevalue']//td[text() = 'Name']]/preceding-sibling::td")).click()


            There are few cases which are possible only with XPath in selenium




            • Finding parent element of an element

            • Finding preceding sibling of an element

            • Finding an element with innerText

            • Finding nth element of the locator


            The above cases are not possible with css selector and xpath is the only straight forward way to find those element.You can also achieve these indirectly with jquery selector and javascript executor.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 11 at 19:52

























            answered Nov 10 at 23:49









            Navarasu

            1,7981721




            1,7981721












            • "[...] find selector which is [...] never changing" - dream on! :)
              – Andrejs
              Nov 11 at 19:16






            • 1




              Ha ha ! Updated the sentence
              – Navarasu
              Nov 11 at 19:53


















            • "[...] find selector which is [...] never changing" - dream on! :)
              – Andrejs
              Nov 11 at 19:16






            • 1




              Ha ha ! Updated the sentence
              – Navarasu
              Nov 11 at 19:53
















            "[...] find selector which is [...] never changing" - dream on! :)
            – Andrejs
            Nov 11 at 19:16




            "[...] find selector which is [...] never changing" - dream on! :)
            – Andrejs
            Nov 11 at 19:16




            1




            1




            Ha ha ! Updated the sentence
            – Navarasu
            Nov 11 at 19:53




            Ha ha ! Updated the sentence
            – Navarasu
            Nov 11 at 19:53


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244005%2fselenium-webdriver-is-it-a-professional-practice-to-use-xpath%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

            Florida Star v. B. J. F.

            Danny Elfman

            Lugert, Oklahoma