I have to stop the browser from loading soon after the click action; is there any work-around for it.. or is there any JS implementation for the same..
            Asked
            
        
        
            Active
            
        
            Viewed 1,521 times
        
    0
            
            
        - 
                    maybe to use prevent default on any click on a tag http://stackoverflow.com/questions/265478/preventdefault-on-an-a-tag – Shimon Doodkin Mar 07 '17 at 14:51
- 
                    I tried this @driver.execute_script("return $('.xyzlocators').click(function(event) { event.preventDefault(); });") – Prashanth Sams Mar 07 '17 at 15:12
- 
                    no luck; anything i have to make changes in it? – Prashanth Sams Mar 07 '17 at 15:13
- 
                    one idea(paste this in console to try it): window.addEventListener("click",function(e){ console.log(e.target, e.path); event.stopPropagation(); event.preventDefault(); }) – Shimon Doodkin Mar 07 '17 at 15:54
2 Answers
0
            
            
        You can use javascript setTimeout and setInterval for delaying your click action.
 <a onclick="delayLoder()">Click</a>
  function delayLoder(){
        setTimeout(function(){      
            window.location = 'https://www.google.com';
        }, 3000);
     }
 
    
    
        Mohit
        
- 1
- 1
- 
                    Actually I am navigating from one page to another page using a link; have to avoid navigation after click action. here is what i do self.firstProperty.click – Prashanth Sams Mar 07 '17 at 14:48
- 
                    window.location is the location that we are currently at ? or the destination ? – Prashanth Sams Mar 07 '17 at 14:49
- 
                    @PrashanthSams it is destination. If you want to stop navigation you can use event.preventDefault() Using about code in answer your page changes after 3 sec. – Mohit Mar 07 '17 at 15:56
0
            
            
        The simplest, workaround I can think of, is to navigate to a 'wrong page' straight after the click. (i.e. http://www.notAvalidHTTPaddress.com). But if this is not fit for purpose, you can alternatively try to use the equivalent for sendkeys in JavaScript in order to hit 'ESC' --> which in Chrome for example will halt page loading.
In Java:
public Keys soloKey(int key) throws AWTException  
{
      Robot r;
        try 
        {
             r = new Robot();
             r.keyPress(key);
             r.keyRelease(key); 
        }
            catch (AWTException e) {
            e.printStackTrace();}
        return null;
}
soloKey(KeyEvent.VK_ESCAPE);
In Javascript:
var keyboardEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keyboardEvent[initMethod](
                   "keydown", // event type : keydown, keyup, keypress
                    true, // bubbles
                    true, // cancelable
                    window, // viewArg: should be window
                    false, // ctrlKeyArg
                    false, // altKeyArg
                    false, // shiftKeyArg
                    false, // metaKeyArg
                    27, // keyCodeArg : unsigned long the virtual key code, else 0
                    0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
document.dispatchEvent(keyboardEvent);
The javaScript answer was taken from here: js-press-key
and here are the keyCodes (27 is ESC) js-key-codes
Hope this helps!
 
    
    
        Community
        
- 1
- 1
 
    
    
        Xwris Stoixeia
        
- 1,831
- 21
- 22
