How to upload a file in Selenium WebDriver with no 'input' element











up vote
3
down vote

favorite
3












I have a HTML page with button named "Upload" and id: btn-import-questions. The element:



<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>


I tried a Selenium Java code like this:



driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");



But since this is an upload button and not an input-type element, the above code is not working.










share|improve this question
























  • What happens when you click this button manually?
    – Eugene S
    Jul 17 '15 at 6:24










  • Window popup will appear immediately and asking for the file path with open and cancel button
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:28










  • And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
    – Eugene S
    Jul 17 '15 at 6:30










  • After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:34










  • Is your window showing uploaded file path..?? Once check it directly on website.
    – Saritha G
    Jul 17 '15 at 6:46















up vote
3
down vote

favorite
3












I have a HTML page with button named "Upload" and id: btn-import-questions. The element:



<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>


I tried a Selenium Java code like this:



driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");



But since this is an upload button and not an input-type element, the above code is not working.










share|improve this question
























  • What happens when you click this button manually?
    – Eugene S
    Jul 17 '15 at 6:24










  • Window popup will appear immediately and asking for the file path with open and cancel button
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:28










  • And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
    – Eugene S
    Jul 17 '15 at 6:30










  • After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:34










  • Is your window showing uploaded file path..?? Once check it directly on website.
    – Saritha G
    Jul 17 '15 at 6:46













up vote
3
down vote

favorite
3









up vote
3
down vote

favorite
3






3





I have a HTML page with button named "Upload" and id: btn-import-questions. The element:



<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>


I tried a Selenium Java code like this:



driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");



But since this is an upload button and not an input-type element, the above code is not working.










share|improve this question















I have a HTML page with button named "Upload" and id: btn-import-questions. The element:



<button class="btn btn-success btn-sm col-lg-11" id="btn-import-questions" data-ts-file-selector="questions-import-init">  Upload&nbsp;<i class="fa fa-upload"></i></button>


I tried a Selenium Java code like this:



driver.findElement(By.id("btn-import-questions")).sendkeys("C:/path/to/file.xlsx");



But since this is an upload button and not an input-type element, the above code is not working.







java selenium testing selenium-webdriver automation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 26 '17 at 10:47









Ripon Al Wasim

25.4k31124150




25.4k31124150










asked Jul 17 '15 at 6:12









Thirunavukarasu Paramasivam

7619




7619












  • What happens when you click this button manually?
    – Eugene S
    Jul 17 '15 at 6:24










  • Window popup will appear immediately and asking for the file path with open and cancel button
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:28










  • And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
    – Eugene S
    Jul 17 '15 at 6:30










  • After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:34










  • Is your window showing uploaded file path..?? Once check it directly on website.
    – Saritha G
    Jul 17 '15 at 6:46


















  • What happens when you click this button manually?
    – Eugene S
    Jul 17 '15 at 6:24










  • Window popup will appear immediately and asking for the file path with open and cancel button
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:28










  • And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
    – Eugene S
    Jul 17 '15 at 6:30










  • After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
    – Thirunavukarasu Paramasivam
    Jul 17 '15 at 6:34










  • Is your window showing uploaded file path..?? Once check it directly on website.
    – Saritha G
    Jul 17 '15 at 6:46
















What happens when you click this button manually?
– Eugene S
Jul 17 '15 at 6:24




What happens when you click this button manually?
– Eugene S
Jul 17 '15 at 6:24












Window popup will appear immediately and asking for the file path with open and cancel button
– Thirunavukarasu Paramasivam
Jul 17 '15 at 6:28




Window popup will appear immediately and asking for the file path with open and cancel button
– Thirunavukarasu Paramasivam
Jul 17 '15 at 6:28












And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
– Eugene S
Jul 17 '15 at 6:30




And when you choose a file using this pop up window, will the name of this file appear somewhere on the page after the pop up disappears?
– Eugene S
Jul 17 '15 at 6:30












After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
– Thirunavukarasu Paramasivam
Jul 17 '15 at 6:34




After providing the file path and click on open, suddenly the popup window disappears and the data in the file are imported to my application.
– Thirunavukarasu Paramasivam
Jul 17 '15 at 6:34












Is your window showing uploaded file path..?? Once check it directly on website.
– Saritha G
Jul 17 '15 at 6:46




Is your window showing uploaded file path..?? Once check it directly on website.
– Saritha G
Jul 17 '15 at 6:46












2 Answers
2






active

oldest

votes

















up vote
1
down vote













You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:




  • Selenium Webdriver - click on hidden elements






share|improve this answer






























    up vote
    1
    down vote













    Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():



    driver.findElement(By.xpath("//input[@type="file"]")).sendkeys(localFilePath);





    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%2f31469386%2fhow-to-upload-a-file-in-selenium-webdriver-with-no-input-element%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
      1
      down vote













      You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:




      • Selenium Webdriver - click on hidden elements






      share|improve this answer



























        up vote
        1
        down vote













        You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:




        • Selenium Webdriver - click on hidden elements






        share|improve this answer

























          up vote
          1
          down vote










          up vote
          1
          down vote









          You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:




          • Selenium Webdriver - click on hidden elements






          share|improve this answer














          You are doing it almost correctly, except that sendKeys() should be called on the input with type="file" that is, most likely invisible in your case. If this is the case, make the element visible first:




          • Selenium Webdriver - click on hidden elements







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 23 '17 at 12:09









          Community

          11




          11










          answered Jul 17 '15 at 12:16









          alecxe

          318k63606831




          318k63606831
























              up vote
              1
              down vote













              Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():



              driver.findElement(By.xpath("//input[@type="file"]")).sendkeys(localFilePath);





              share|improve this answer



























                up vote
                1
                down vote













                Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():



                driver.findElement(By.xpath("//input[@type="file"]")).sendkeys(localFilePath);





                share|improve this answer

























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():



                  driver.findElement(By.xpath("//input[@type="file"]")).sendkeys(localFilePath);





                  share|improve this answer














                  Check the DOM because somewhere there must be an <input type="file">. The website's javascript will call the .click() of this element to pop up the file selector dialog and closing the dialog with a selection will provide the path. With Selenium the same can be achieved with the .sendkeys():



                  driver.findElement(By.xpath("//input[@type="file"]")).sendkeys(localFilePath);






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 15 at 13:53

























                  answered Nov 15 at 13:26









                  Visko

                  73118




                  73118






























                      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%2f31469386%2fhow-to-upload-a-file-in-selenium-webdriver-with-no-input-element%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

                      Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

                      ComboBox Display Member on multiple fields

                      Is it possible to collect Nectar points via Trainline?