Manivannan Nagarajan
Manivannan Nagarajan

Reputation: 1059

How to make JavaScript Snippets work after page reloads, in Chrome?

Environment: Chrome, a simple Web Portal

I'm trying to do a simple browser automation.

In a HTML table, 1. Clicks the first link, 2. Change the dropdown value and 3. click Submit.

The code after button click is not executed, as the page loads after the button click.

var tbl = document.getElementById("incident_table")
tbl.rows[2].cells[2].getElementsbyClassName("linked")[0].click()
// below code is not executing, as the above click loads the page
document.getElementById("incident.state").selectedIndex = 1
document.getElementById("sysverb_update").click()

I can able to run the last 2 lines of code separately in console, it works.

But when executing as a snippet it didnt

enter image description here

Upvotes: 0

Views: 610

Answers (1)

Hkrie
Hkrie

Reputation: 127

this post is very similar to your question: preventDefault() on an <a> tag

what you want to do is prevent the default action of the javascript click event.

What you may also do is:

tbl.rows[2].cells[2].getElementsbyClassName("linked")[0].addEventListener('click', ()=>{
    preventDefault();
    //write here what should happen instead
    });

What this will do is prevent the default action "reload site" from happening

Edit: What i mean in the comment is the following:

if(localStorage.getItem("firstPartComplete") === null){
   //do first part
   //set completion status in localStorage
   localStorage.setItem("firstPartComplete", true);
}else{
   // do second part
   localStorage.removeItem("firstPartComplete");
}

Upvotes: 1

Related Questions