Is it possible to upload file with selenium through anchor tag?











up vote
0
down vote

favorite












I have an upload file button looks like this: <a href="javascript:void('');"></a>



Trying to upload file through it using sendKeys() method.
But I get errors cannot focus on element. Is there's a workaround or the way to upload file with <a> tag?



I use this:



driver.findElementsByXPath("//a[@title='Upload an image']").get(0).sendKeys(System.getProperty("user.dir") + "filesimage.jpg");


Tried also to click(), to moveToElement(element).build().perform() to focus on the element.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have an upload file button looks like this: <a href="javascript:void('');"></a>



    Trying to upload file through it using sendKeys() method.
    But I get errors cannot focus on element. Is there's a workaround or the way to upload file with <a> tag?



    I use this:



    driver.findElementsByXPath("//a[@title='Upload an image']").get(0).sendKeys(System.getProperty("user.dir") + "filesimage.jpg");


    Tried also to click(), to moveToElement(element).build().perform() to focus on the element.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have an upload file button looks like this: <a href="javascript:void('');"></a>



      Trying to upload file through it using sendKeys() method.
      But I get errors cannot focus on element. Is there's a workaround or the way to upload file with <a> tag?



      I use this:



      driver.findElementsByXPath("//a[@title='Upload an image']").get(0).sendKeys(System.getProperty("user.dir") + "filesimage.jpg");


      Tried also to click(), to moveToElement(element).build().perform() to focus on the element.










      share|improve this question















      I have an upload file button looks like this: <a href="javascript:void('');"></a>



      Trying to upload file through it using sendKeys() method.
      But I get errors cannot focus on element. Is there's a workaround or the way to upload file with <a> tag?



      I use this:



      driver.findElementsByXPath("//a[@title='Upload an image']").get(0).sendKeys(System.getProperty("user.dir") + "filesimage.jpg");


      Tried also to click(), to moveToElement(element).build().perform() to focus on the element.







      java selenium selenium-webdriver file-upload anchor






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      DebanjanB

      35.5k73271




      35.5k73271










      asked Nov 19 at 19:24







      user10664410































          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          No it won't be possible to upload a file with selenium through <a> tag.



          File uploading is achieved through:





          • <input> tags.

          • These <input> tags needs focus before being filled up by any character sequence


          On the contrary,





          • The attributes of an <a> tag are are as follows:





            • charset: Defines the character encoding of the linked document.


            • coords: Specifies the coordinates appropriate to the shape attribute to define a region of an image for image maps


            • href: Specifies the URL of a page or the name of the anchor that the link goes to.


            • hreflang: Language code of the destination URL.


            • name: Marks an area of the page that a link jumps to.


            • rel: Describes the relationship between the current document and the destination URI.


            • rev: Specifies the relationship between the target URL and the current document.


            • shape: Specifies the shape of the image map.


            • target: Where to open the target URL.


            • type: Specifies the MIME (Multipurpose Internet Mail Extensions) type of the target URL


            • accesskey: Access keys (or shortcut keys).


            • class: Document wide identifier.


            • dir: Specifies the direction of the text.


            • id: Document wide identifier.


            • tabindex: Helps determine the tabbing order when the user 'tabs' through the elements on the page.


            • title: Specifies a title to associate with the element.


            • style: Helps to include inline casecadubf style sheet.


            • lang: Sets the language code.




          • The event attributes of an <a> tag are are as follows:





            • onfocus: Script runs when the element gets focus.


            • onblur: Script runs when the element loses focus.


            • onclick: Script runs when a mouse click.


            • ondblclick: Script runs when a mouse double-click.


            • onmousedown: Script runs when mouse button is pressed.


            • onmouseup: Script runs when mouse button is released.


            • onmouseover: Script runs when mouse pointer moves over an element.


            • onmousemove: Script runs when mouse pointer moves.


            • onmouseout: Script runs when mouse pointer moves out of an element.


            • onkeypress: Script runs when key is pressed and released.


            • onkeydown: Script runs when key is pressed.


            • onkeyup: Script runs when key is released.




          So, ideally the <a> tags should also be able to receive the focus.



          But as you are seeing an error as ...cannot focus on element... it may be due to other factors which is out of scope for this question.



          Conclusion



          File uploading have to be done through <input> tags only.



          Reference




          • Anchors

          • HTML A tag






          share|improve this answer



















          • 1




            @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
            – DebanjanB
            Nov 20 at 3:25


















          up vote
          0
          down vote













          You need to handle input field of type file, but not link:



          driver.findElementByXPath("//input[@type='file']").sendKeys(System.getProperty("user.dir") + "filesimage.jpg");





          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%2f53381313%2fis-it-possible-to-upload-file-with-selenium-through-anchor-tag%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
            0
            down vote



            accepted










            No it won't be possible to upload a file with selenium through <a> tag.



            File uploading is achieved through:





            • <input> tags.

            • These <input> tags needs focus before being filled up by any character sequence


            On the contrary,





            • The attributes of an <a> tag are are as follows:





              • charset: Defines the character encoding of the linked document.


              • coords: Specifies the coordinates appropriate to the shape attribute to define a region of an image for image maps


              • href: Specifies the URL of a page or the name of the anchor that the link goes to.


              • hreflang: Language code of the destination URL.


              • name: Marks an area of the page that a link jumps to.


              • rel: Describes the relationship between the current document and the destination URI.


              • rev: Specifies the relationship between the target URL and the current document.


              • shape: Specifies the shape of the image map.


              • target: Where to open the target URL.


              • type: Specifies the MIME (Multipurpose Internet Mail Extensions) type of the target URL


              • accesskey: Access keys (or shortcut keys).


              • class: Document wide identifier.


              • dir: Specifies the direction of the text.


              • id: Document wide identifier.


              • tabindex: Helps determine the tabbing order when the user 'tabs' through the elements on the page.


              • title: Specifies a title to associate with the element.


              • style: Helps to include inline casecadubf style sheet.


              • lang: Sets the language code.




            • The event attributes of an <a> tag are are as follows:





              • onfocus: Script runs when the element gets focus.


              • onblur: Script runs when the element loses focus.


              • onclick: Script runs when a mouse click.


              • ondblclick: Script runs when a mouse double-click.


              • onmousedown: Script runs when mouse button is pressed.


              • onmouseup: Script runs when mouse button is released.


              • onmouseover: Script runs when mouse pointer moves over an element.


              • onmousemove: Script runs when mouse pointer moves.


              • onmouseout: Script runs when mouse pointer moves out of an element.


              • onkeypress: Script runs when key is pressed and released.


              • onkeydown: Script runs when key is pressed.


              • onkeyup: Script runs when key is released.




            So, ideally the <a> tags should also be able to receive the focus.



            But as you are seeing an error as ...cannot focus on element... it may be due to other factors which is out of scope for this question.



            Conclusion



            File uploading have to be done through <input> tags only.



            Reference




            • Anchors

            • HTML A tag






            share|improve this answer



















            • 1




              @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
              – DebanjanB
              Nov 20 at 3:25















            up vote
            0
            down vote



            accepted










            No it won't be possible to upload a file with selenium through <a> tag.



            File uploading is achieved through:





            • <input> tags.

            • These <input> tags needs focus before being filled up by any character sequence


            On the contrary,





            • The attributes of an <a> tag are are as follows:





              • charset: Defines the character encoding of the linked document.


              • coords: Specifies the coordinates appropriate to the shape attribute to define a region of an image for image maps


              • href: Specifies the URL of a page or the name of the anchor that the link goes to.


              • hreflang: Language code of the destination URL.


              • name: Marks an area of the page that a link jumps to.


              • rel: Describes the relationship between the current document and the destination URI.


              • rev: Specifies the relationship between the target URL and the current document.


              • shape: Specifies the shape of the image map.


              • target: Where to open the target URL.


              • type: Specifies the MIME (Multipurpose Internet Mail Extensions) type of the target URL


              • accesskey: Access keys (or shortcut keys).


              • class: Document wide identifier.


              • dir: Specifies the direction of the text.


              • id: Document wide identifier.


              • tabindex: Helps determine the tabbing order when the user 'tabs' through the elements on the page.


              • title: Specifies a title to associate with the element.


              • style: Helps to include inline casecadubf style sheet.


              • lang: Sets the language code.




            • The event attributes of an <a> tag are are as follows:





              • onfocus: Script runs when the element gets focus.


              • onblur: Script runs when the element loses focus.


              • onclick: Script runs when a mouse click.


              • ondblclick: Script runs when a mouse double-click.


              • onmousedown: Script runs when mouse button is pressed.


              • onmouseup: Script runs when mouse button is released.


              • onmouseover: Script runs when mouse pointer moves over an element.


              • onmousemove: Script runs when mouse pointer moves.


              • onmouseout: Script runs when mouse pointer moves out of an element.


              • onkeypress: Script runs when key is pressed and released.


              • onkeydown: Script runs when key is pressed.


              • onkeyup: Script runs when key is released.




            So, ideally the <a> tags should also be able to receive the focus.



            But as you are seeing an error as ...cannot focus on element... it may be due to other factors which is out of scope for this question.



            Conclusion



            File uploading have to be done through <input> tags only.



            Reference




            • Anchors

            • HTML A tag






            share|improve this answer



















            • 1




              @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
              – DebanjanB
              Nov 20 at 3:25













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            No it won't be possible to upload a file with selenium through <a> tag.



            File uploading is achieved through:





            • <input> tags.

            • These <input> tags needs focus before being filled up by any character sequence


            On the contrary,





            • The attributes of an <a> tag are are as follows:





              • charset: Defines the character encoding of the linked document.


              • coords: Specifies the coordinates appropriate to the shape attribute to define a region of an image for image maps


              • href: Specifies the URL of a page or the name of the anchor that the link goes to.


              • hreflang: Language code of the destination URL.


              • name: Marks an area of the page that a link jumps to.


              • rel: Describes the relationship between the current document and the destination URI.


              • rev: Specifies the relationship between the target URL and the current document.


              • shape: Specifies the shape of the image map.


              • target: Where to open the target URL.


              • type: Specifies the MIME (Multipurpose Internet Mail Extensions) type of the target URL


              • accesskey: Access keys (or shortcut keys).


              • class: Document wide identifier.


              • dir: Specifies the direction of the text.


              • id: Document wide identifier.


              • tabindex: Helps determine the tabbing order when the user 'tabs' through the elements on the page.


              • title: Specifies a title to associate with the element.


              • style: Helps to include inline casecadubf style sheet.


              • lang: Sets the language code.




            • The event attributes of an <a> tag are are as follows:





              • onfocus: Script runs when the element gets focus.


              • onblur: Script runs when the element loses focus.


              • onclick: Script runs when a mouse click.


              • ondblclick: Script runs when a mouse double-click.


              • onmousedown: Script runs when mouse button is pressed.


              • onmouseup: Script runs when mouse button is released.


              • onmouseover: Script runs when mouse pointer moves over an element.


              • onmousemove: Script runs when mouse pointer moves.


              • onmouseout: Script runs when mouse pointer moves out of an element.


              • onkeypress: Script runs when key is pressed and released.


              • onkeydown: Script runs when key is pressed.


              • onkeyup: Script runs when key is released.




            So, ideally the <a> tags should also be able to receive the focus.



            But as you are seeing an error as ...cannot focus on element... it may be due to other factors which is out of scope for this question.



            Conclusion



            File uploading have to be done through <input> tags only.



            Reference




            • Anchors

            • HTML A tag






            share|improve this answer














            No it won't be possible to upload a file with selenium through <a> tag.



            File uploading is achieved through:





            • <input> tags.

            • These <input> tags needs focus before being filled up by any character sequence


            On the contrary,





            • The attributes of an <a> tag are are as follows:





              • charset: Defines the character encoding of the linked document.


              • coords: Specifies the coordinates appropriate to the shape attribute to define a region of an image for image maps


              • href: Specifies the URL of a page or the name of the anchor that the link goes to.


              • hreflang: Language code of the destination URL.


              • name: Marks an area of the page that a link jumps to.


              • rel: Describes the relationship between the current document and the destination URI.


              • rev: Specifies the relationship between the target URL and the current document.


              • shape: Specifies the shape of the image map.


              • target: Where to open the target URL.


              • type: Specifies the MIME (Multipurpose Internet Mail Extensions) type of the target URL


              • accesskey: Access keys (or shortcut keys).


              • class: Document wide identifier.


              • dir: Specifies the direction of the text.


              • id: Document wide identifier.


              • tabindex: Helps determine the tabbing order when the user 'tabs' through the elements on the page.


              • title: Specifies a title to associate with the element.


              • style: Helps to include inline casecadubf style sheet.


              • lang: Sets the language code.




            • The event attributes of an <a> tag are are as follows:





              • onfocus: Script runs when the element gets focus.


              • onblur: Script runs when the element loses focus.


              • onclick: Script runs when a mouse click.


              • ondblclick: Script runs when a mouse double-click.


              • onmousedown: Script runs when mouse button is pressed.


              • onmouseup: Script runs when mouse button is released.


              • onmouseover: Script runs when mouse pointer moves over an element.


              • onmousemove: Script runs when mouse pointer moves.


              • onmouseout: Script runs when mouse pointer moves out of an element.


              • onkeypress: Script runs when key is pressed and released.


              • onkeydown: Script runs when key is pressed.


              • onkeyup: Script runs when key is released.




            So, ideally the <a> tags should also be able to receive the focus.



            But as you are seeing an error as ...cannot focus on element... it may be due to other factors which is out of scope for this question.



            Conclusion



            File uploading have to be done through <input> tags only.



            Reference




            • Anchors

            • HTML A tag







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered Nov 19 at 19:35









            DebanjanB

            35.5k73271




            35.5k73271








            • 1




              @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
              – DebanjanB
              Nov 20 at 3:25














            • 1




              @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
              – DebanjanB
              Nov 20 at 3:25








            1




            1




            @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
            – DebanjanB
            Nov 20 at 3:25




            @Maybellene Updated the answer with more details and reference. Upvote the answer if this/any answer is/was helpful to you for the benefit of the future readers.
            – DebanjanB
            Nov 20 at 3:25












            up vote
            0
            down vote













            You need to handle input field of type file, but not link:



            driver.findElementByXPath("//input[@type='file']").sendKeys(System.getProperty("user.dir") + "filesimage.jpg");





            share|improve this answer

























              up vote
              0
              down vote













              You need to handle input field of type file, but not link:



              driver.findElementByXPath("//input[@type='file']").sendKeys(System.getProperty("user.dir") + "filesimage.jpg");





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                You need to handle input field of type file, but not link:



                driver.findElementByXPath("//input[@type='file']").sendKeys(System.getProperty("user.dir") + "filesimage.jpg");





                share|improve this answer












                You need to handle input field of type file, but not link:



                driver.findElementByXPath("//input[@type='file']").sendKeys(System.getProperty("user.dir") + "filesimage.jpg");






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 19:34









                Andersson

                34.9k103066




                34.9k103066






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53381313%2fis-it-possible-to-upload-file-with-selenium-through-anchor-tag%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