Tried with CSS selector and xpath but nothing is working for the button that i need to click











up vote
0
down vote

favorite












I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."



HTML CODE:



<div class="details__selectItem"> 
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>


I tried with following locators:



driver.findElement(By.linkText("Select Lens Type")).click();

driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();

driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();

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


enter image description here










share|improve this question




















  • 1




    Post your code.
    – Guy
    Nov 22 at 5:43






  • 1




    @Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
    – DebanjanB
    Nov 22 at 6:48










  • These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
    – KjetilNordin
    Nov 22 at 15:36










  • @KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
    – Rozmeen
    Nov 22 at 15:39















up vote
0
down vote

favorite












I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."



HTML CODE:



<div class="details__selectItem"> 
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>


I tried with following locators:



driver.findElement(By.linkText("Select Lens Type")).click();

driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();

driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();

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


enter image description here










share|improve this question




















  • 1




    Post your code.
    – Guy
    Nov 22 at 5:43






  • 1




    @Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
    – DebanjanB
    Nov 22 at 6:48










  • These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
    – KjetilNordin
    Nov 22 at 15:36










  • @KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
    – Rozmeen
    Nov 22 at 15:39













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."



HTML CODE:



<div class="details__selectItem"> 
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>


I tried with following locators:



driver.findElement(By.linkText("Select Lens Type")).click();

driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();

driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();

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


enter image description here










share|improve this question















I am trying to locate an element thats actually a button but every time it shows "Unable to locate such element."



HTML CODE:



<div class="details__selectItem"> 
<a href="/product/lens?sku=4234_BLACKCRYSTAL" class="btn primary block" style="">
Select Lens Type
</a>
</div>


I tried with following locators:



driver.findElement(By.linkText("Select Lens Type")).click();

driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[2]/div/div[3]")).click();

driver.findElement(By.cssSelectors("a.primary.block[href='/product/lens?sku=4234_BLACKCRYSTAL']")).click();

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


enter image description here







selenium webdriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 16:37









Skynet

4,24652640




4,24652640










asked Nov 22 at 3:10









Rozmeen

2816




2816








  • 1




    Post your code.
    – Guy
    Nov 22 at 5:43






  • 1




    @Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
    – DebanjanB
    Nov 22 at 6:48










  • These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
    – KjetilNordin
    Nov 22 at 15:36










  • @KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
    – Rozmeen
    Nov 22 at 15:39














  • 1




    Post your code.
    – Guy
    Nov 22 at 5:43






  • 1




    @Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
    – DebanjanB
    Nov 22 at 6:48










  • These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
    – KjetilNordin
    Nov 22 at 15:36










  • @KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
    – Rozmeen
    Nov 22 at 15:39








1




1




Post your code.
– Guy
Nov 22 at 5:43




Post your code.
– Guy
Nov 22 at 5:43




1




1




@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48




@Rozmeen Please read why a screenshot of HTML or code or error is a bad idea. Consider updating the Question with formatted text based relevant HTML, code trials and error stack trace.
– DebanjanB
Nov 22 at 6:48












These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36




These method calls seem correct, although a little complicated due to the html not being selenium friendly. Maybe try findElements instead of singular, and see which elements are returned? I mostly prefer cssSelector myself. You could get all a.primary.block for instance and if more exist, see which ones it returns? When I have problems finding elements, it is most often because they are generated by some javascript, and are not available at the time I try to find them. Selenium have waiting functionality which lets you wait until the elements are loaded. I suspect that might be the error?
– KjetilNordin
Nov 22 at 15:36












@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39




@KjetilNordin, this is the error trace: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"link text","selector":"SelectLensType"}
– Rozmeen
Nov 22 at 15:39












1 Answer
1






active

oldest

votes

















up vote
0
down vote













The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:



int timeoutSeconds = 30;
By locator = By.id("element-id"); //use any 'By' here

WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
driver.findElement(locator).click();


Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:



wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
wait.until(ExpectedConditions.elementToBeClickable(locator));


If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.






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',
    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%2f53423328%2ftried-with-css-selector-and-xpath-but-nothing-is-working-for-the-button-that-i-n%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:



    int timeoutSeconds = 30;
    By locator = By.id("element-id"); //use any 'By' here

    WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
    wait.until(ExpectedConditions.presenceOfElementLocated(locator));
    driver.findElement(locator).click();


    Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:



    wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
    wait.until(ExpectedConditions.elementToBeClickable(locator));


    If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.






    share|improve this answer

























      up vote
      0
      down vote













      The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:



      int timeoutSeconds = 30;
      By locator = By.id("element-id"); //use any 'By' here

      WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
      wait.until(ExpectedConditions.presenceOfElementLocated(locator));
      driver.findElement(locator).click();


      Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:



      wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
      wait.until(ExpectedConditions.elementToBeClickable(locator));


      If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:



        int timeoutSeconds = 30;
        By locator = By.id("element-id"); //use any 'By' here

        WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        driver.findElement(locator).click();


        Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:



        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
        wait.until(ExpectedConditions.elementToBeClickable(locator));


        If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.






        share|improve this answer












        The most likely scenario, is that the element simply does not exist when you try to find it. Especially since you have tried four different selectors. This might happen when the page is not fully loaded when you try to locate the element. You can implement waiting functionality, which will wait a specified number of seconds, for the element to exist, be visible, or be clickable. I am not sure which language your Selenium is in, but here is a Java version:



        int timeoutSeconds = 30;
        By locator = By.id("element-id"); //use any 'By' here

        WebDriverWait wait = new WebDriverWait(driver, timeoutSeconds);
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        driver.findElement(locator).click();


        Alternatively, you can wait for clickable or visible, using this line instead of the second last in the example above:



        wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
        wait.until(ExpectedConditions.elementToBeClickable(locator));


        If you are using another language than Java, you will have to search the api of the specific language, to find the correct syntax. As far as I know, Selenium is not identical across languages, but I will expect this to be available in most of them.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 15:47









        KjetilNordin

        1,40042968




        1,40042968






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53423328%2ftried-with-css-selector-and-xpath-but-nothing-is-working-for-the-button-that-i-n%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

            Catalogne

            Violoncelliste

            Héron pourpré