簡體   English   中英

如何使用 Python 或 Robot 框架單擊打印預覽頁面上的打印按鈕

[英]How to Click on Print button on Print preview page using Python or Robot framework

我有一個網站,是否需要驗證打印功能。 我點擊了網站上的打印圖標,它打開了一個帶有打印預覽頁面的新窗口。 在此頁面中,我需要單擊打印圖標。以下是我嘗試使用機器人框架 + 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

打印函數.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:消息:沒有這樣的元素:無法定位元素:{"method":"css selector","selector":"print-preview-app"}

有人可以幫我嗎?

如果沒有看到頁面的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM