自定义关键字机器人框架 selenium webdriver python "object has no attribute 'get_attribute' "

Custom keyword robot framework selenium webdriver python "object has no attribute 'get_attribute' "

我正在尝试使用 python 中现有的 Seleniumlibrary 机器人框架编写自定义函数,以从元素中获取 link。但我一直在 get_attribute.

中遇到问题

Error : 'list' object has no attribute 'get_attribute'

库已导入

from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn

def get_one_links(locator,attribute):
    lib = BuiltIn().get_library_instance('SeleniumLibrary')
    links = lib.find_elements(locator).get_attribute(attribute)
    return links

那是因为你试图调用列表上的方法,你只能调用单个元素。请参见下面的示例。 "get_attribute" 属性 不存在于列表中,但 "get_attribute" 属性 存在于单个元素中。例如:

您需要在代码中执行类似的操作,

from selenium import webdriver
from robot.libraries.BuiltIn import BuiltIn

def get_one_links(locator,attribute):
    lib = BuiltIn().get_library_instance('SeleniumLibrary')
    links = lib.find_elements(locator)
    for link in links:
        return link.get_attribute('href')
        #return link if thats what you want