1

I'm trying to simply (in a JavaScript environment managed in HTML script tags) load a webpage and press a button on that page. I also want to be able to read data off of the webpage, but that should be self-explanatory once I get button pressing working. I've looked around for solutions but none seem to work.

For sake of a controlled environment, I have 2 simple websites. On the first there is a button which calls a JavaScript function, in which I attempt to load website 2 and press another button. On website 2, there is a single button that, when pressed, calls a JavaScript function and generates a dialogue box.

I can load page 2 using window.open(), and I believe that has a return type which is a handle to the website, which I save. I've tried using that handle to call the javascript function in webpage 2. I've tried getting the element by ID and using click()/submit(). And here's the code to the current attempt:

Website 1:

<html>
    <body>
        <script  language = "javaScript">
            function print() {
                //alert('Hello World');

                var win = window.open("file:///Users/Jacob/Documents/2ndWebTest.html");

                win.document.form[0].submit();
            }
        </script>
        <p><button onclick = "print()">test</button></p>
    </body>
</html>

Website 2:

<html>
    <body>
        <script  language = "javaScript">
            function testFunc() {
                alert('Hello World');
            }
        </script>
        <p><button onclick = "testFunc() id = "buttonTest"">test</button></p>
    </body>
</html>

I'm trying to navigate through a website hierarchy and pull data from it. I think this should at least work in theory, but I can't get access to any of the functions or elements on website 2. What am I doing wrong?

j08691
  • 204,283
  • 31
  • 260
  • 272
Jean Valjean
  • 747
  • 1
  • 9
  • 30

1 Answers1

1

In your above example the web server will throw an access denied error unless you give it access to the folder in which your target file sits.

On my local computer I was able to get your example to work by setting up a virtual directory called TestSite and place your two files into it. I access the other one with this command:

 var win = window.open("localhost/TestSite/2ndWebTest.html");

And I also fix the double quotes in line 8 in file 2ndWebTest.html to read like:

<p><button onclick = "testFunc()" id = "buttonTest">test</button></p>
Jon Wilson
  • 86
  • 6