【问题标题】:How to dismiss print dialog in selenium?如何关闭硒中的打印对话框?
【发布时间】:2016-09-09 12:24:57
【问题描述】:

我正在尝试渲染一个加载时自动显示打印对话框的页面。我想使用硒跳过它。我在互联网上搜索但找不到任何合适的示例,因为大多数示例处理 javascript 警报而不是 Windows 警报。

我还安装了 python robotframework,因为很多人建议使用它,但也找不到任何示例。

我的问题是如何在 python 中使用 selenium 和 robotsframework 关闭 Windows 警报?

注意:这个问题是我之前问题的延续。 python selenium not updating pop up window url

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver robotframework


    【解决方案1】:

    我可以通过覆盖window.print 函数来避免打印窗口:

    driver.execute_script("window.print = function(){};")
    

    【讨论】:

    • 将它放在 get 命令之后和点击操作之前。请注意,每次重新加载/刷新页面都会重置打印功能。
    • 没有点击功能。打印对话框在页面加载时自动出现。我只是想跳过它。我尝试将它放在之后和之前,但它不会跳过打印对话框。
    【解决方案2】:

    使用 Firefox,您可以设置一些配置文件选项来有效地使打印对话框弹出静音。

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    
    profile = webdriver.FirefoxProfile()
    profile.set_preference("print.always_print_silent", True)
    profile.set_preference("print.show_print_progress", False)
    
    driver = webdriver.Firefox(profile)
    driver.get("http://www.google.com")
    
    # Send print instruction
    elem = driver.find_element_by_xpath("//body")
    elem.send_keys(Keys.COMMAND, "p")
    

    【讨论】:

    • 这将接受打印对话框并打印页面。我想跳过/拒绝它而不打印页面。
    猜你喜欢
    • 2021-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 2016-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多