在chrome最新版本中使用硒处理打印预览窗口 [英] Handle Print Preview window using selenium in chrome latest version

查看:134
本文介绍了在chrome最新版本中使用硒处理打印预览窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Google chrome 75.0.3770.80中的打印"对话框. 我正在使用硒关闭打印对话框上的取消按钮.

I'm trying to work with a Print Dialog box in google chrome version 75.0.3770.80. I am clicking on cancel button on print dialog using Selenium to close it.

可以检查取消"按钮,并且其选择器在UI上可见,但是当我尝试使用硒单击那些选择器时,没有此类元素异常.

The cancel button can be inspected and its selectors are visible on the UI but when I am trying to click on those selectors using selenium it is given No such element exception.

此外,当我使用该页面的getSource()时,按钮选择器不在源代码中,但在UI上可见

Also, when I am using getSource() for that page button selectors are not present in the source code but are visible on UI

那么,我们如何单击取消按钮,有什么方法可以做到这一点?.

So, how can we click on cancel button is there any way to do this?.

推荐答案

这是python中的解决方案.您可以将此方法转换为Java.

Here is the solution in python. You can convert this method to java.

def cancelPrintPreview():
    # get the current time and add 180 seconds to wait for the print preview cancel button
    endTime = time.time() + 180
    # switch to print preview window
    driver.switch_to.window(driver.window_handles[-1])
    while True:
        try:
            # get the cancel button
            cancelButton = driver.execute_script(
                "return document.querySelector('print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-header#header').shadowRoot.querySelector('paper-button.cancel-button')")
            if cancelButton:
                # click on cancel
                cancelButton.click()
                # switch back to main window
                driver.switch_to.window(driver.window_handles[0])
                return True
        except:
            pass
        time.sleep(1)
        if time.time() > endTime:
            driver.switch_to.window(driver.window_handles[0])
            break

您可以查看我的答案此处以获取有关使用阴影根元素的更多信息.

You can check my answer here for more information on working with shadow-root elements.

这篇关于在chrome最新版本中使用硒处理打印预览窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆