Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.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 RobotFramework-如何解析警报中的每个span元素并检查它是否包含文本?_Python_Selenium_Selenium Webdriver_Robotframework - Fatal编程技术网

Python RobotFramework-如何解析警报中的每个span元素并检查它是否包含文本?

Python RobotFramework-如何解析警报中的每个span元素并检查它是否包含文本?,python,selenium,selenium-webdriver,robotframework,Python,Selenium,Selenium Webdriver,Robotframework,因此,我有一个警报,它有多个span元素,带有不同的错误消息。我需要测试该警报中是否包含指定的消息。例如,在下面的屏幕截图中,span元素之一包含“需要电子邮件字段”消息。我尝试用Python实现它,然后在RobotFramework中将它用作关键字,但我一直在寻找当前的浏览器实例。Parser.py文件包含: from selenium.webdriver.common.by import By from selenium import webdriver def parse_alert_ms

因此,我有一个警报,它有多个span元素,带有不同的错误消息。我需要测试该警报中是否包含指定的消息。例如,在下面的屏幕截图中,span元素之一包含“需要电子邮件字段”消息。我尝试用Python实现它,然后在RobotFramework中将它用作关键字,但我一直在寻找当前的浏览器实例。Parser.py文件包含:

from selenium.webdriver.common.by import By
from selenium import webdriver
def parse_alert_msg(elements, message):

    alerts = driver.find_elements(By.XPATH, elements)
    for item in alerts:
        if item.text == message:
            return True
    return False 
这是我的机器人文件中的内容:

Test Function
    Open Website
    Login Successfully as       ${UserName}     ${Password}
    ${message}  Parser.Parse Alert Msg  //div[@class='alert-content']/span[@class='alert-description']  You have logged in.
    Run Keyword If  ${message}=${FALSE}  Fail  There is no such span with that text.
    [Teardown]  Close Browser

好的,这就是我想到的解决方案:

Check If Alert Contains
    [Arguments]  ${alert-path}  ${alert-message}
    @{alert-msg}  Get Web Elements  ${alert-path}
    : FOR  ${element}  IN  @{alert-msg}
    \   ${element_text}  Get Text  ${element}
    \   Run Keyword If   '${element_text}' == '${alert-message}'  Exit For Loop
    Run Keyword Unless  '${element_text}' == '${alert-message}'  Fail  The message "${alert-message}" is not found.
如果有人能找到更好的方法,请随时发布:)

编辑: 我觉得这是我能得到的最好的答案。无需循环和/或实现过于复杂的逻辑

Wait Until Alert Contains
    [Arguments]  ${message}
    Wait Until Element Is Visible   //div[@class='alert-content']
    Element Should Contain  //div[@class='alert-content']      ${message}

在我看来,不需要循环,因为您已经可以使用xpath
text()
函数执行检查:

Check If Alert Contains Errormessage
    [Arguments]  ${alert-message}
    Page Should Contain Element    
    ...    xpath://div[@class='alert-content']/span[@class='alert-description' and text()='${alert-message}']   
    ...    message=The message "${alert-message}" is not found.

所以问题实际上是“如何在Python方法中使用RF浏览器实例”,因为您已经有了检查文本的算法?顺便说一句,为什么不在纯Robotframework中实现check-in,这几乎是相同的流程-获取循环的所有元素的kw是
获取Webelements
。我对编程的观点更为敏感,但我肯定会尝试。回到你的问题-如果只是如何获取对正在运行的浏览器的引用-这是,即使这个例子是专门针对SeleniumLibrary的,但我建议用Robotframework语法实现关键字,特别是对于像这样的简单任务。否则,当功能在RF中不可用或太麻烦而无法实现时,您将失去许多is好处—详细的日志记录、统一的语言等。Python libs/方法确实有其用途—但这种情况不符合描述:)可能重复