Selenium: finding the right element identifier











up vote
1
down vote

favorite












I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:



<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">


I thought I'd use the "id" of the box, but the code:



Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService

Dim wd As New Chrome.ChromeDriver(service, options)

wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")


Gives an error:



Unable to locate element: {"method":"id","selector":"txtEndCustId"}


To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.



Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?










share|improve this question
























  • Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
    – Ryan Wilson
    Nov 21 at 15:52












  • Sorry, it's just the chromedriver object - I've expanded the sample code now
    – Martin KS
    Nov 21 at 16:01










  • IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
    – Ryan Wilson
    Nov 21 at 16:07












  • no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
    – Martin KS
    Nov 21 at 16:14










  • @MartingKS is the element you want wrapped inside a frame, iframe?
    – Ryan Wilson
    Nov 21 at 16:18















up vote
1
down vote

favorite












I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:



<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">


I thought I'd use the "id" of the box, but the code:



Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService

Dim wd As New Chrome.ChromeDriver(service, options)

wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")


Gives an error:



Unable to locate element: {"method":"id","selector":"txtEndCustId"}


To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.



Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?










share|improve this question
























  • Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
    – Ryan Wilson
    Nov 21 at 15:52












  • Sorry, it's just the chromedriver object - I've expanded the sample code now
    – Martin KS
    Nov 21 at 16:01










  • IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
    – Ryan Wilson
    Nov 21 at 16:07












  • no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
    – Martin KS
    Nov 21 at 16:14










  • @MartingKS is the element you want wrapped inside a frame, iframe?
    – Ryan Wilson
    Nov 21 at 16:18













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:



<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">


I thought I'd use the "id" of the box, but the code:



Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService

Dim wd As New Chrome.ChromeDriver(service, options)

wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")


Gives an error:



Unable to locate element: {"method":"id","selector":"txtEndCustId"}


To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.



Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?










share|improve this question















I'm relatively new to selenium automation, and am trying to test a form that has been created for me. I'd like to put test values into a textbox whose HTML code looks like:



<input type="text" id="txtEndCustId" onkeypress="return isNumberKey(event);" class="Text" required="" onfocus="txtEndCustIdFocus('Your customer END CUSTOMER DEP ID');" onblur="txtEndCustIdblur();">


I thought I'd use the "id" of the box, but the code:



Dim options As New Chrome.ChromeOptions
Dim service As ChromeDriverService = ChromeDriverService.CreateDefaultService

Dim wd As New Chrome.ChromeDriver(service, options)

wd.Navigate.GoToUrl("x")
wd.FindElementById("txtEndCustId").SendKeys("1")


Gives an error:



Unable to locate element: {"method":"id","selector":"txtEndCustId"}


To be perfectly honest I've no idea where I went wrong here - thanks in advance for your help.



Is there any "List All Element ids" function, so that I can marry them up with what's shown in the chrome inspector view?







vb.net selenium selenium-chromedriver






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 16:00

























asked Nov 21 at 15:51









Martin KS

141218




141218












  • Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
    – Ryan Wilson
    Nov 21 at 15:52












  • Sorry, it's just the chromedriver object - I've expanded the sample code now
    – Martin KS
    Nov 21 at 16:01










  • IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
    – Ryan Wilson
    Nov 21 at 16:07












  • no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
    – Martin KS
    Nov 21 at 16:14










  • @MartingKS is the element you want wrapped inside a frame, iframe?
    – Ryan Wilson
    Nov 21 at 16:18


















  • Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
    – Ryan Wilson
    Nov 21 at 15:52












  • Sorry, it's just the chromedriver object - I've expanded the sample code now
    – Martin KS
    Nov 21 at 16:01










  • IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
    – Ryan Wilson
    Nov 21 at 16:07












  • no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
    – Martin KS
    Nov 21 at 16:14










  • @MartingKS is the element you want wrapped inside a frame, iframe?
    – Ryan Wilson
    Nov 21 at 16:18
















Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 at 15:52






Please provide the entire line of code where you are trying to get the element by it's ID, (...) does not give enough information.
– Ryan Wilson
Nov 21 at 15:52














Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 at 16:01




Sorry, it's just the chromedriver object - I've expanded the sample code now
– Martin KS
Nov 21 at 16:01












IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 at 16:07






IS it possible you are trying to get the element before the page has fully loaded?? You may want to read this SO post on how to do a wait with webdriver (stackoverflow.com/questions/13244225/…)
– Ryan Wilson
Nov 21 at 16:07














no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 at 16:14




no, I'm stepping through the code in the debugger, and it doesn't even allow me to step to that line before the page has finished the 'navigate' line and fully loaded - or at least it appears visually to be fully loaded, and the chromium window can see the element if I hit F12 there.
– Martin KS
Nov 21 at 16:14












@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 at 16:18




@MartingKS is the element you want wrapped inside a frame, iframe?
– Ryan Wilson
Nov 21 at 16:18












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.



First switch to the frame:



//You need to use method switchTo() to set the driver to the frame document
//In this case we are passing in the id of the iframe
wd.switchTo().frame("ctl00_CPH_iframeCat");

//Now that the driver is working on the frame document, you should be able to manipulate the
//input you want
wd.FindElementById("txtEndCustId").SendKeys("1")

//Finally, switch the driver back to the main page document (original document which contains the frames)
driver.switchTo().defaultContent();


Here is a good piece of documentation which should explain this in more detail:
(http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)






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%2f53415788%2fselenium-finding-the-right-element-identifier%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
    1
    down vote



    accepted










    Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.



    First switch to the frame:



    //You need to use method switchTo() to set the driver to the frame document
    //In this case we are passing in the id of the iframe
    wd.switchTo().frame("ctl00_CPH_iframeCat");

    //Now that the driver is working on the frame document, you should be able to manipulate the
    //input you want
    wd.FindElementById("txtEndCustId").SendKeys("1")

    //Finally, switch the driver back to the main page document (original document which contains the frames)
    driver.switchTo().defaultContent();


    Here is a good piece of documentation which should explain this in more detail:
    (http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)






    share|improve this answer

























      up vote
      1
      down vote



      accepted










      Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.



      First switch to the frame:



      //You need to use method switchTo() to set the driver to the frame document
      //In this case we are passing in the id of the iframe
      wd.switchTo().frame("ctl00_CPH_iframeCat");

      //Now that the driver is working on the frame document, you should be able to manipulate the
      //input you want
      wd.FindElementById("txtEndCustId").SendKeys("1")

      //Finally, switch the driver back to the main page document (original document which contains the frames)
      driver.switchTo().defaultContent();


      Here is a good piece of documentation which should explain this in more detail:
      (http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)






      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.



        First switch to the frame:



        //You need to use method switchTo() to set the driver to the frame document
        //In this case we are passing in the id of the iframe
        wd.switchTo().frame("ctl00_CPH_iframeCat");

        //Now that the driver is working on the frame document, you should be able to manipulate the
        //input you want
        wd.FindElementById("txtEndCustId").SendKeys("1")

        //Finally, switch the driver back to the main page document (original document which contains the frames)
        driver.switchTo().defaultContent();


        Here is a good piece of documentation which should explain this in more detail:
        (http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)






        share|improve this answer












        Since your element is inside a frame, you need to navigate the driver to the frame first, a frame has it's own Document containing x number of DOM elements.



        First switch to the frame:



        //You need to use method switchTo() to set the driver to the frame document
        //In this case we are passing in the id of the iframe
        wd.switchTo().frame("ctl00_CPH_iframeCat");

        //Now that the driver is working on the frame document, you should be able to manipulate the
        //input you want
        wd.FindElementById("txtEndCustId").SendKeys("1")

        //Finally, switch the driver back to the main page document (original document which contains the frames)
        driver.switchTo().defaultContent();


        Here is a good piece of documentation which should explain this in more detail:
        (http://www.assertselenium.com/webdriver/handling-iframes-using-webdriver/)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 at 16:52









        Ryan Wilson

        3,3701418




        3,3701418






























            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%2f53415788%2fselenium-finding-the-right-element-identifier%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

            Berounka

            Sphinx de Gizeh

            Different font size/position of beamer's navigation symbols template's content depending on regular/plain...