如何使用Python或Robot框架在“打印预览”页面上单击“打印”按钮

时间:2019-12-19 12:59:42

标签: python selenium selenium-webdriver robotframework

我有一个网站,需要验证打印功能吗?我单击了网站上的打印图标,它为我打开了一个带有打印预览页面的新窗口。在此页面中,我需要单击“打印”图标。下面是我在使用机器人框架+ Python尝试的代码

Sampleprint.robot

*** Settings ***
Library    Selenium2Library
Library    printfunc.py

*** Test Case ***
Validate Downloads Page Title
    Open Browser    http://samplewebsite.com   chrome
    wait until page contains element  //*[@id="introduction-container"]  10s
    keypress
    sleep  4s
    ${output}=    Get Title Present Under Shadow Root Element

printfunc.py

from robot.libraries.BuiltIn import BuiltIn


def expand_shadow_element(driver, element):
    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
    return shadow_root


def get_title_present_under_shadow_root_element():
    selenium2lib = BuiltIn().get_library_instance('Selenium2Library')
    # following line returns webdriver initiated in robot-framework
    driver = selenium2lib.driver

    # # shadow root locator - preceding tag of #shadow-root
    root1 = driver.find_element_by_tag_name('print-preview-app')
    shadow_root1 = expand_shadow_element(driver, root1)
    return shadow_root1

执行此脚本时,它总是在错误消息下方显示给我

NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“ css选择器”,“选择器”:“ print-preview-app”}

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

我无法准确地判断页面的html,但这很可能是iframe的影响。

检查要查找的元素上方是否有iframe,如果有,请参见有关从以下来源切换iframe的部分

from robot.libraries.BuiltIn import BuiltIn

def expand_shadow_element(driver, element):
    shadow_root = driver.execute_script('return arguments[0].shadowRoot', element)
    return shadow_root

def get_title_present_under_shadow_root_element():
    selenium2lib = BuiltIn().get_library_instance('Selenium2Library')
    # following line returns webdriver initiated in robot-framework
    driver = selenium2lib.driver

    #add iframe
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))

    # # shadow root locator - preceding tag of #shadow-root
    root1 = driver.find_element_by_tag_name('print-preview-app')
    shadow_root1 = expand_shadow_element(driver, root1)
    return shadow_root1