单击pyqt按钮后立即停止加载页面selenium

时间:2017-09-24 04:00:25

标签: python python-3.x selenium selenium-webdriver pyqt5

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

由于这个页面花费了太多时间加载我想在我点击一个pyQt按钮并开始填写表格后立即停止加载该页面。我不想使用browser.set_page_load_timeout因为我不知道何时停止

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)

1 个答案:

答案 0 :(得分:1)

您需要使用pageLoadStrategy作为none加载Firefox。然后你可以停止页面加载,但这样做的副作用是你需要等待所有地方自己加载页面,因为selenium不会自动为你做这件事。你也可以使用eager而不是none来等待readyState of page为true,但仍然可以给你一些时间加载页面

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();")

编辑-1:旧版本的Firefox

对于旧版本的firefox,您需要设置配置文件首选项,因为这些浏览器不使用marrionette

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