0

I wrote the following code which I run in Chrome Dev-Tools:

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function demo() {
    counter = 1;
    while (counter <= 363) {
        document.getElementsByClassName('print')[1].click()
        await sleep(5000);
        document.getElementsByName('ctl00$PopupBody$printButtons')[0].click();
        document.getElementsByName('ctl00$PopupBody$txtPageStart')[0].value = counter;
        document.getElementsByName('ctl00$PopupBody$txtPageEnd')[0].value = counter + 10;
        document.getElementsByClassName('ConfirmButton')[0].click();
        counter+=10;
        await sleep(5000);
    }
}
demo()

I want to convert a file in a website into PDF so I use the print option to convert it to PDF. It allows up to 10 page each print so I want to write a code which does it for me. Basically it should open a model each time, fill the fields and click confirm (which close the modal). The problem is that after document.getElementsByClassName('print')[1].click() it opens a modal and for some reason document can't find ctl00$PopupBody$printButtons. I guess it does not render the DOM. How can I "refresh" document to use the current HTML?

EDIT: window.location.reload(true); (and other methods like this one) do full page reload, the modal closes and the code stops running (reload dev-tools). I don't want to reload the page, just the HTML in document

abuka123
  • 441
  • 3
  • 12
  • 2
    Does this answer your question? [How to reload a page using JavaScript](https://stackoverflow.com/questions/3715047/how-to-reload-a-page-using-javascript) – lissettdm Dec 05 '20 at 14:55
  • @lissettdm It does not work. `window.location.reload(true);` (and other methods like this one) do full page reload, the modal closes and the code stops running. I don't want to reload the page, just the HTML in document. – abuka123 Dec 05 '20 at 15:33
  • BTW if someone wants to try running the code himself: [link](https://school.kotar.cet.ac.il/KotarApp/Viewer.aspx?nBookID=97607368&nTocEntryID=97610121#348.4815.6.default) – abuka123 Dec 05 '20 at 15:36
  • 1
    Ok, but you asked how to force the document to refresh, so you need to refresh one document's section, am I right? – lissettdm Dec 05 '20 at 15:50
  • @lissettdm Maybe my choice of words is poor but I think that it can be understood from context. If I want the code to continue looping I can't reload the whole page (it will reload dev-tools as well). I need a way for the document to contain the "new" HTML once the modal is opened. So I guess that I need to re-render the HTML in `document`. The question is how to do it. Reloading the whole page, does not help much. By "force document to refresh" I mean "refresh `document` object without reloading the page. I described what I tried to do to solve it. Maybe solution can be obtain in other way. – abuka123 Dec 05 '20 at 16:36

0 Answers0