简体   繁体   中英

Selenium stop page load as soon as expected element exist

I created few test cases on Selenium using Java. Unfortunately when I click an element on the page, before I could move on to any other action, I have to wait till the page loads.

I have tried driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); . Unfortunately this creates another problem. Even if the page loads, it waits 30 seconds before it started testing on the page.

What I found the best way is to send ESCAPE key to stop page load.

Is there anyway I could check if an element exists and when it does, send ESCAPE key to the browser to stop page load?

This part is bugging my mind as I have to wait till page loads before Java reads the next line of the code so I can't send ESCAPE key to browser till the page actually stops loading.

Edit

I have just tried using a new thread to do the job but it seems driver is completely locked out, can't do any process on it before page stops loading.

I'm out of ideas for the moment but I believe there should be a way.

Using timeouts() is causing whole test case to stop.

First I'd like to say this isn't a best practice. The selenium click method states that if the click triggers a page load, selenium will do its best to block until the page is loaded. Instead of clicking via the click method you could try sending the click event via JavaScript. Then wait for the element like normal.

You can try driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); It is supposed to throw an error after timeout is over. I have never used it but maybe you can try and catch this error and continue with your test. But your page could end up in an unstable state with everything not loading and test interacts with elements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM