参考地址:https://blog.csdn.net/max229max/article/details/82852155
Robot Framwork中Selenium2Library已升级到SeleniumLibrary同样没有提供在通过get webelement获得已有元素列表中查找元素的方法,比如在用户列表框中通过勋章属性确定用户类别,并获取用户名
查找了很多资料只有一个类似的需求参考,Selenium2Library的版本.python升级SeleniumLibrary很多用法不一样,参考代码运行不通.自己研究了一番参考源码,找到两种解决方案:
①在D:\Program Files\Python36\Lib\site-packages\SeleniumLibrary\keywords\element.py源码中参考get_webelement添加两个函数传入parent即可通过父元素查找子元素
class ElementKeywords(LibraryComponent):
# 从父元素中查找子元素文字信息
@keyword(name='Get Text In Element')
def get_text_in_element(self, element, locator):
"""
:paramelement:
:paramlocator:
:return:
Example:
| ${text}= | `Get Text In Element` | element | locator(xpath=.//div[@class="break"]) |
"""
return self.find_element(locator,parent=element).text
# 从父元素获取属性值
@keyword(name='Get Attribute In Element')
def get_attribute_in_element(self, element, locator, attribute):
"""Returns the value of ``attribute`` from the element ``locator``.
See the `Locating elements` section for details about the locator
syntax.
Example:
| ${id}= | `Get Element Attribute` | css:h1 | id |
"""
return self.find_element(locator,parent=element).get_attribute(attribute)
@keyword(name='Get WebElement')
def get_webelement(self, locator):
"""Returns the first WebElement matching the given ``locator``.
See the `Locating elements` section for details about the locator
syntax.
"""
return self.find_element(locator)
②参考源码自己编写工具包
图片如下
from selenium.webdriver.remote.webelementimport WebElement
from SeleniumLibrary.locatorsimport ElementFinder
import SeleniumLibrary
try:
str()# attempt to evaluate basestring
def isstr(s):
return isinstance(s, str)
except NameError:
def isstr(s):
return isinstance(s, str)
class ElementUtil(SeleniumLibrary.SeleniumLibrary):
def finder(self):
driver = ElementFinder(self)
return driver
# 通过元素查找元素获取文本值
def get_text_in_ele(element, locator):
"""Returns the first child WebElement matching the given locator in given parent WebElement.
element: should be an parent element
locator: should be an child locator string
See `introduction` for details about locating elements.
Examples:
| ${element} | Get Text In Element | parent element | child locator |
"""
return _element_find_in_element(element, locator, True, True).text
# Python3中获取属性方式,通过元素查找元素获取属性值
def get_element_attribute_in_element(element, locator, attribute_name):
"""Returns the value of ``attribute`` from the element ``locator``.
See the `Locating elements` section for details about the locator
syntax.
Example:
| ${id}= | `Get Element Attribute In Element` | parent | css:h1 | id |
Passing attribute name as part of the ``locator`` was removed
in SeleniumLibrary 3.2. The explicit ``attribute`` argument
should be used instead.
"""
# ek = ElementKeywords()
#升级Python3后locator和属性名需单独传入
# locator, attribute_name = ek.parse_attribute_locator(attribute_locator)
# locator, attribute_name = ElementFinder(self)._parse_locator(attribute_locator)
# print(locator,attribute_name)
element = _element_find_in_element(element, locator, True, False)
if elementis None:
raise ValueError("Element '%s' not found." % (locator))
return element.get_attribute(attribute_name)
# 私有方法元素查找元素,继承SeleniumLibrary.SeleniumLibrary创建元素查找实例ElementFinder
def _element_find_in_element(element, locator, first_only, required, tag=None):
print(type(element))
if isinstance(element, WebElement):
browser = element
if isstr(locator):
try:
driver = ElementUtil().finder()
elements = driver.find(locator, tag=tag, first_only=first_only, required=required, parent=browser)
return elements
except:
pass
两种方案都运行成功了