Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python或Robot框架单击打印预览页面上的打印按钮_Python_Selenium_Selenium Webdriver_Robotframework - Fatal编程技术网

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

如何使用Python或Robot框架单击打印预览页面上的打印按钮,python,selenium,selenium-webdriver,robotframework,Python,Selenium,Selenium Webdriver,Robotframework,我有一个网站,我需要验证打印功能。我点击了一个网站上的打印图标,它为我打开了一个带有打印预览页面的新窗口。在这个页面中,我需要点击打印图标 机器人 *** Settings *** Library Selenium2Library Library printfunc.py *** Test Case *** Validate Downloads Page Title Open Browser http://samplewebsite.com chrome

我有一个网站,我需要验证打印功能。我点击了一个网站上的打印图标,它为我打开了一个带有打印预览页面的新窗口。在这个页面中,我需要点击打印图标

机器人

*** 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选择器”,“选择器”:“打印预览应用程序”}


有人能帮我吗?

如果没有看到页面的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

谢谢你的回复。但那个html页面中并没有iframe。我正在查看您的示例代码,但并没有看到您切换到新窗口的位置。Selenium无法看到新窗口何时打开,您必须使用关键字切换到该窗口。