【问题标题】:stop loading page selenium as soon as pyqt button is clicked单击 pyqt 按钮后立即停止加载页面 selenium
【发布时间】:2017-09-27 01:16:40
【问题描述】:
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)

【问题讨论】:

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


    【解决方案1】:

    您需要使用pageLoadStrategy 将firefox 加载为none。然后您可以停止页面加载,但这样做的副作用是您需要自己在所有地方等待页面加载,因为 selenium 不会自动为您执行此操作。您也可以使用eager 而不是none,这将等待页面的readyState 为真,但仍然给您一些时间来加载页面

    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:旧版本的 Firefox

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

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

    【讨论】:

    • 效果很好。谢谢。你能不能告诉我你从哪里如此深入地掌握了硒
    • @palakbansal,没有什么特别的,只是阅读其他人分享的内容并分享我所知道的内容
    • 嘿@TarunLalwani 这在 Firefox 55 版和 selenium 3.4.3 中对我来说非常有效,但我有一个限制,在工作场所我必须在 firefox 28 或 firefox 32 和 selenium 2.41 中工作。还有你的代码等到页面完全加载。我准备明确等待元素。你能帮我吗:)
    • @palakbansal,也为旧版本添加了答案
    • 谢谢我明天试试这个:)
    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 2016-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多