代码之家  ›  专栏  ›  技术社区  ›  Rakesh

尝试从自定义库访问关键字时,显示错误InvalidArgumentException

  •  0
  • Rakesh  · 技术社区  · 6 年前

    我使用下面的文件夹结构来维护我的测试脚本

    Test_Scripts
    
        TestCase
            TestSuite1.robot
    
        SupportFiles
            RF_CustomLibrary.py
    

    测试套件1.robot

    *** Settings ***
    Library                             SeleniumLibrary
    Library                            ..\\SupportFiles\\RF_CustomLibrary.py
    
    *** Variables ***
    ${Browser}     Chrome
    
    *** Test cases ***
    
    Sample Test Case
        Verify Paste text functionality
    
    *** Keywords ***
    Verify Paste text functionality
        Set Library Search Order    RF_CustomLibrary
        Open Browser    https://gmail.com    ${BROWSER}
        Sleep    2s
        Maximize Browser Window
        Wait Until Keyword Succeeds    60s     2s   Element Should Be Visible       ${L_Login_btn}
        PasteTextFunction    id=identifierId    Username1
    

    自定义库:RF_CustomLibrary.py

    import os
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from SeleniumLibrary import SeleniumLibrary
    from SeleniumLibrary.base import LibraryComponent, keyword
    from SeleniumLibrary.errors import ElementNotFound
    from SeleniumLibrary.utils import is_noney
    
    
    class RF_CustomLibrary(SeleniumLibrary):
    
        @keyword
        def pasteTextFunction(self, locator, text):
            os.system("echo off | clip")
            os.system("echo %s| clip" % text.strip())
            element = self._current_browser().find_element(locator)
            element.clear()
            element.send_keys(Keys.CONTROL, 'v')
    

    当我执行此测试用例时,将为关键字“显示以下错误消息” PasteTextFunction函数 "

    InvalidArgumentException: Message: invalid argument: invalid locator
    

    任何解决此错误的建议/输入都会很有帮助。

    0 回复  |  直到 6 年前
        1
  •  4
  •   Bryan Oakley    6 年前

    您正在调用低级selenium函数 find_element ,但是传递了一个SeleniumLibrary风格的定位器( id=identifierId )这不是低级别的selenium驱动程序能够理解的。

    如果您想使用硒元素定位器,您需要使用 Get webelement 硒化法。

    element = self.get_webelement(locator)
    
    推荐文章