Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am doing a lot of insertions into a Web page's DOM through Javascript, but need to do more adjustments (preferably) in Perl.

Is there a way to access the DOM from within Perl?

Thanks.

Replies are listed 'Best First'.
Re: accessing DOM from Perl?
by ikegami (Patriarch) on Jul 02, 2009 at 00:06 UTC

    It's not very clear what you mean due to insufficient details.

    • Are you asking

      Is there a way to access the DOM tree in a web browser from a CGI script written in Perl?

      Then no. Code on one machine can't manipulate a data structure that hasn't even been created yet in another machine.

    • Are you asking

      Can Perl be executed as a client-side script in a web browser, and if so, can it access the DOM tree?

      There is software to do that, but it's rarely installed and might only work on Windows machines.

      So unless the client machines are under your control, the answer is no.

    • Are you asking something else?

Re: accessing DOM from Perl?
by ambrus (Abbot) on Jul 02, 2009 at 20:43 UTC

    Sure, you could use any HTML module to manipulate the tree structure of the web page before you serve it from the server. I recommend XML::Twig. Could you be a bit more specific about what you want?

Re: accessing DOM from Perl?
by spx2 (Deacon) on Jul 02, 2009 at 12:57 UTC
    As ikegami pointed out , you should be more specific , so because you are not specific I will make what I want of your question and consider that you want to write some Perl script that will use WWW::Selenium to control Selenium which will in turn control a browser by injecting js in it at runtime. So , quoting the WWW::Selenium documentation :
    1)$sel->get_eval($script) Gets the result of evaluating the specified JavaScript snippet.
    2)$sel->run_script($script) Creates a new "script" tag in the body of the current test window, and + adds the specified text into the body of the command. Scripts run in +this way can often be debugged more easily than scripts executed usin +gSelenium's "getEval" command. Beware that JS exceptions thrown in th +ese scripttags aren't managed by Selenium, so you should probably wra +p your scriptin try/catch blocks if there is any chance that the scri +pt will throwan exception.
    You can download Selenium here , you want to download Selenium RC. Good luck !