如何在selenium中取消打印对话框?

5
我正在尝试渲染一个页面,它在加载时会自动显示打印对话框。我想使用selenium跳过它。我在互联网上搜索,但没有找到任何合适的例子,因为大多数例子处理的是javascript警报而不是窗口警报。
我还安装了python的robotframework,因为许多人建议这样做,但也找不到任何示例。
我的问题是如何使用selenium和python中的robotframework取消窗口警报?
注:此问题是我以前的问题的延续。python selenium not updating pop up window url
2个回答

1
我可以通过覆盖 window.print 函数来避免打印窗口:
driver.execute_script("window.print = function(){};")

将其放置在获取命令之后,单击操作之前。请注意,每次重新加载/刷新页面都会重置打印功能。 - Florent B.
2
没有点击功能。打印对话框会在页面加载时自动出现。我只想跳过它。我尝试将其放在之前和之后,但它并不能跳过打印对话框。 - Manish Gupta

1
使用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")

这段代码将接受打印对话框并打印页面。我想要跳过/拒绝它,而不打印页面。 - Manish Gupta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接