跳转到主要内容

Selenium的一个(相当)智能定位类。

项目描述

selenium-smart-locator

Build Status Coverage Status Code Health

封装生成Selenium定位器的类。

它旨在简单直观,提供了多种生成与Selenium兼容的定位器的方法。该类可用于Selenium元素查询。

loc = Locator(By.XPATH, '//foo/bar/baz')    # Old selenium way of doing it
element = s.find_element(*loc)              # Expand the tuple. That's how to use the class.

这是一个基本示例,展示了其工作原理。现在来看看简化定位器的使用示例

# When you use this simple format of CSS consisting of tag name, ids and classes, it gets
# detected automatically and the result is a CSS selector. IDs and classes are optional.
Locator('div#foo.bar.baz')  # => Locator(by='css selector', locator='div#foo.bar.baz')
# When you specify a plain string and it does not get matched be the preceeding CSS detector
# it is assumed it is an XPath expression
Locator('//h1') # => Locator(by='xpath', locator='//h1')
# If you pass a Locator instance, it just goes straight through
Locator(Locator('//h1')) # => Locator(by='xpath', locator='//h1')
# If you have your own object, that implements __locator__(), then it can also be resolved
# by the class. The __locator__() must either return Locator instance or
# anything that Locator can process.
Locator(my_obj)
# You can leverage kwargs to say strategy=locator
Locator(xpath='//h1')   # => Locator(by='xpath', locator='//h1')
Locator(css='#something')   # => Locator(by='css selector', locator='#something')
Locator(by='xpath', locator='//h1')   # => Locator(by='xpath', locator='//h1')
# For your comfort, you can pass a dictionary, like it was kwargs
Locator({'by': 'xpath', 'locator': '//h1'})   # => Locator(by='xpath', locator='//h1')
# You can also use Locator's classmethods, like this:
Locator.by_css('#foo')   # => Locator(by='css selector', locator='#foo')
# Always in format Locator.by_<strategy_name>

当您有定位器时,您可以通过使用便利方法来避免使用*

l = Locator('#foo')
browser = Firefox()
element = l.find_element(browser)
elements = l.find_elements(browser)

如您所见,指定输入参数的方法数量为您提供了极大的自由度来构建您的定位器。您可以将它们存储在YAML中,并使用定位器来解析条目。或者任何其他方式。

可用的选择器策略

  • class_name

  • css

  • id

  • link_text

  • name

  • partial_link_text

  • tag

  • xpath

许可协议

遵循GPLv3许可

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

selenium-smart-locator-0.3.0.tar.gz (19.0 kB 查看哈希值)

上传时间 源代码

构建分发

selenium_smart_locator-0.3.0-py3-none-any.whl (17.5 kB 查看哈希值)

上传时间 Python 3

由以下机构支持