0

I want to access a web page through javascript using 'XMLHttpRequest', grab the output and retrieve the table information from the output using 'getElementbyID' or 'getElementsbyName' methods. I could get to the output but the following code but retrieval of the table info gives an error.

var webPage = 'xx';
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var reqWebPage = new XMLHttpRequest();
reqWebPage.open('GET', webPage, false);
reqWebPage.send();
var responseHTTP = reqWebPage.responseText;
var tableMain = responseHTTP.getElementbyID('main-table');
console.log tableMain

Error:TypeError: responseHTTP.getElementsbyName is not a function. Is there a way to get around this? I am running this javascript on node and not the browser.

  • 2
    You can try this: [https://stackoverflow.com/questions/13466812/getelementbyid-not-a-function-when-trying-to-parse-an-ajax-response](https://stackoverflow.com/questions/13466812/getelementbyid-not-a-function-when-trying-to-parse-an-ajax-response) Or isn't that what u want? – onno204 Aug 01 '17 at 21:53
  • You can also try `var responseHTTP = reqWebPage.responseText; var div = document.createElement('div'); div.innerHTML = responseHTTP; var tableMain = div.getElementbyID('main-table');` – Will Aug 01 '17 at 21:57
  • @Will - But doesn't your suggestion assume that a document object will be present. That is not the case when parsing an AJAX response. – EJK Aug 01 '17 at 22:05
  • onno204's suggestion looks like the right way to go. – EJK Aug 01 '17 at 22:06
  • @onno204 - That still points to working on a browser. On a browser, I could use document object. I just parsed the output through a dom-parser to get the required info. – intrepid_atom Aug 02 '17 at 23:25

1 Answers1

-1

I think that responseText have not a getElementbyID method, you should get the "main-table" element by iterating on child nodes of reqWebPage.responseText

AngeLOL
  • 106
  • 2
  • 9
  • 1
    Parsed the responseText using the dom-parser to get the required table details. var DomParser = require('dom-parser'); var parser = new DomParser(); var dom = parser.parseFromString(responseHTTP); var element = dom.getElementById('main-table').innerHTML; – intrepid_atom Aug 02 '17 at 23:22