javascript get a query string that a form will submit












1














If you have a form, like



<form id='testform' method="post" action="/" />
<input name="test1" value="yes" type="text">
<input name="test2" value="no" type="text">
</form>


Is there a way to get the query string that this form would submit? (without knowing anything about what fields are inside it) test1=yes&test2=no or just the same thing in json format?



I am using selenium, so could do it either on the selenium side run javascript in the browser.



const form = await driver.findElement(By.id('testform'));
.. now do something in selenium to find out the request post request


or in the browser



const form = document.getElementById('testform');
... somehow get the query string or request info


any ideas?










share|improve this question






















  • Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
    – Patrick Evans
    Nov 21 '18 at 16:37












  • Do you want just a string from all input fields name-value pairs?
    – Andersson
    Nov 21 '18 at 16:55










  • yeah - string, or object
    – Alex028502
    Nov 21 '18 at 17:06










  • I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
    – Alex028502
    Nov 21 '18 at 17:07


















1














If you have a form, like



<form id='testform' method="post" action="/" />
<input name="test1" value="yes" type="text">
<input name="test2" value="no" type="text">
</form>


Is there a way to get the query string that this form would submit? (without knowing anything about what fields are inside it) test1=yes&test2=no or just the same thing in json format?



I am using selenium, so could do it either on the selenium side run javascript in the browser.



const form = await driver.findElement(By.id('testform'));
.. now do something in selenium to find out the request post request


or in the browser



const form = document.getElementById('testform');
... somehow get the query string or request info


any ideas?










share|improve this question






















  • Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
    – Patrick Evans
    Nov 21 '18 at 16:37












  • Do you want just a string from all input fields name-value pairs?
    – Andersson
    Nov 21 '18 at 16:55










  • yeah - string, or object
    – Alex028502
    Nov 21 '18 at 17:06










  • I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
    – Alex028502
    Nov 21 '18 at 17:07
















1












1








1







If you have a form, like



<form id='testform' method="post" action="/" />
<input name="test1" value="yes" type="text">
<input name="test2" value="no" type="text">
</form>


Is there a way to get the query string that this form would submit? (without knowing anything about what fields are inside it) test1=yes&test2=no or just the same thing in json format?



I am using selenium, so could do it either on the selenium side run javascript in the browser.



const form = await driver.findElement(By.id('testform'));
.. now do something in selenium to find out the request post request


or in the browser



const form = document.getElementById('testform');
... somehow get the query string or request info


any ideas?










share|improve this question













If you have a form, like



<form id='testform' method="post" action="/" />
<input name="test1" value="yes" type="text">
<input name="test2" value="no" type="text">
</form>


Is there a way to get the query string that this form would submit? (without knowing anything about what fields are inside it) test1=yes&test2=no or just the same thing in json format?



I am using selenium, so could do it either on the selenium side run javascript in the browser.



const form = await driver.findElement(By.id('testform'));
.. now do something in selenium to find out the request post request


or in the browser



const form = document.getElementById('testform');
... somehow get the query string or request info


any ideas?







javascript html forms selenium






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 16:27









Alex028502

1,219617




1,219617












  • Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
    – Patrick Evans
    Nov 21 '18 at 16:37












  • Do you want just a string from all input fields name-value pairs?
    – Andersson
    Nov 21 '18 at 16:55










  • yeah - string, or object
    – Alex028502
    Nov 21 '18 at 17:06










  • I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
    – Alex028502
    Nov 21 '18 at 17:07




















  • Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
    – Patrick Evans
    Nov 21 '18 at 16:37












  • Do you want just a string from all input fields name-value pairs?
    – Andersson
    Nov 21 '18 at 16:55










  • yeah - string, or object
    – Alex028502
    Nov 21 '18 at 17:06










  • I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
    – Alex028502
    Nov 21 '18 at 17:07


















Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
– Patrick Evans
Nov 21 '18 at 16:37






Do you actually need the string of data or just to send it somewhere, eg through ajax? If the latter you can use FormData to grab all of a form's input fields and send through ajax directly
– Patrick Evans
Nov 21 '18 at 16:37














Do you want just a string from all input fields name-value pairs?
– Andersson
Nov 21 '18 at 16:55




Do you want just a string from all input fields name-value pairs?
– Andersson
Nov 21 '18 at 16:55












yeah - string, or object
– Alex028502
Nov 21 '18 at 17:06




yeah - string, or object
– Alex028502
Nov 21 '18 at 17:06












I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
– Alex028502
Nov 21 '18 at 17:07






I'm need it in selenium, but so if selenium had a way to do it that would be best, but if there is a way to get it in the browser, I can just use executeScript.
– Alex028502
Nov 21 '18 at 17:07














1 Answer
1






active

oldest

votes


















0














Thanks Patrick,



const formFields = await driver.executeScript('return Array.from(new FormData(document.getElementById("testform")))');


works great.



I guess if anybody needs to do this without selenium it's just



Array.from(new FormData(document.getElementById("testform")))


or look at the other ways you can cycle through the FormData object.



Then you can turn that into a query string if that is what you need.






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',
    autoActivateHeartbeat: false,
    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%2f53416500%2fjavascript-get-a-query-string-that-a-form-will-submit%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









    0














    Thanks Patrick,



    const formFields = await driver.executeScript('return Array.from(new FormData(document.getElementById("testform")))');


    works great.



    I guess if anybody needs to do this without selenium it's just



    Array.from(new FormData(document.getElementById("testform")))


    or look at the other ways you can cycle through the FormData object.



    Then you can turn that into a query string if that is what you need.






    share|improve this answer


























      0














      Thanks Patrick,



      const formFields = await driver.executeScript('return Array.from(new FormData(document.getElementById("testform")))');


      works great.



      I guess if anybody needs to do this without selenium it's just



      Array.from(new FormData(document.getElementById("testform")))


      or look at the other ways you can cycle through the FormData object.



      Then you can turn that into a query string if that is what you need.






      share|improve this answer
























        0












        0








        0






        Thanks Patrick,



        const formFields = await driver.executeScript('return Array.from(new FormData(document.getElementById("testform")))');


        works great.



        I guess if anybody needs to do this without selenium it's just



        Array.from(new FormData(document.getElementById("testform")))


        or look at the other ways you can cycle through the FormData object.



        Then you can turn that into a query string if that is what you need.






        share|improve this answer












        Thanks Patrick,



        const formFields = await driver.executeScript('return Array.from(new FormData(document.getElementById("testform")))');


        works great.



        I guess if anybody needs to do this without selenium it's just



        Array.from(new FormData(document.getElementById("testform")))


        or look at the other ways you can cycle through the FormData object.



        Then you can turn that into a query string if that is what you need.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 9:37









        Alex028502

        1,219617




        1,219617






























            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%2f53416500%2fjavascript-get-a-query-string-that-a-form-will-submit%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'