palak bansal
palak bansal

Reputation: 23

stop loading page selenium as soon as pyqt button is clicked

from selenium import webdriver
driver=webdriver.Firefox()
driver.get("https://9gag.com")#page that take too much time to load

Since this page took too much time to load I want to stop loading that page as soon as I click a pyQt button and start filling form.I dont want to use browser.set_page_load_timeout as I dont know when to stop

def stop_page_load():
    global driver
    """What should be the definition so that as soon as I
       click btn4,browser should stop loading"""



###w is a QWidget
btn4=QtWidgets.QPushButton("stop page load",w)
btn4.pressed.connect(stop_page_load)

Upvotes: 2

Views: 606

Answers (1)

Tarun Lalwani
Tarun Lalwani

Reputation: 146610

You need to load firefox with pageLoadStrategy as none. Then you can stop the page loading, but the side effect of this is that you will need to wait for loading of page yourself at all places as selenium won't do that automatically for you. You can also use eager instead of none which would wait for readyState of page to be true, but still give you sometime for page to load

import time
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium import webdriver

cap = DesiredCapabilities.FIREFOX
cap["pageLoadStrategy"] = "none"
print(DesiredCapabilities.FIREFOX)
driver = webdriver.Firefox(capabilities=cap)

driver.get("https://9gag.com")#page that take too much time to load

time.sleep(1)
driver.execute_script("window.stop();")

Edit-1: Older version of firefox

For older version of firefox you need to set the profile preference as these browser don't use marrionette

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("webdriver.load.strategy", "unstable");
WebDriver driver = new FirefoxDriver(profile);

Upvotes: 1

Related Questions