SeleniumLibrary4.5.0 关键字详解(三)
SeleniumLibrary4.5.0 关键字详解(三)
库版本:4.5.0
库范围:全局
命名参数:受支持
简介
SeleniumLibrary是Robot Framework的Web测试库。
本文档说明了如何使用SeleniumLibrary提供的关键字。 有关安装,支持等信息,请参见 python3.9.0 + robotframework + selenium3 实例体验。
有关robotframework框架的更多信息,请参见 https://blog.csdn.net/mask5726/category_10537277.html。
SeleniumLibrary在内部使用Selenium WebDriver模块来控制Web浏览器。 有关常规Selenium的更多信息,请参见Selenium。
内容列表
1、元素定位
2、浏览器和窗口
3、超时,等待和延迟
4、运行故障功能
5、布尔参数
6、webDriver事件
7、线程支持
8、插件
9、引入
11、关键字
超时、等待和延迟
本节讨论了如何等待元素出现在网页上以及以其他方式降低执行速度的不同方式。它还说明了设置各种超时,等待和延迟时可以使用的时间格式。
超时(timeout)
SeleniumLibrary包含各种关键字,这些关键字具有可选的超时参数,该参数指定这些关键字应等待某些事件或操作的时间。这些关键字包括例如Wait …关键字和与告警相关的关键字。另外,执行异步Javascript。尽管它没有超时参数,但它使用超时来定义异步JavaScript可以运行多长时间。
这些关键字使用的默认超时可以通过使用Set Selenium Timeout关键字来全局设置,也可以在导入库时与timeout参数一起设置。有关支持的超时语法,请参见下面的时间格式。
隐式等待(Implicit wait)
隐式等待指定搜索元素时Selenium最多等待多长时间。导入库时,可以使用Set Selenium Implicit Wait关键字来设置,也可以使用implicit_wait参数来设置。有关此功能的更多信息,请参见Selenium文档。
有关支持的语法,请参见下面的时间格式。
selenium执行速度
可以通过使用Set Selenium speed关键字全局降低Selenium的执行速度。此功能旨在用于演示或调试目的。使用它来确保元素出现在页面上不是一个好主意。应改用上述超时和等待。
有关支持的语法,请参见下面的时间格式。
时间格式
所有超时和等待时间都可以以秒为单位的数字(例如0.5或42)或使用Robot Framework的时间语法(例如1.5 seconds or 1 min 30 s)来给出。有关时间语法的更多信息,请参见《 Robot Framework用户指南》。
故障运行功能
SeleniumLibrary具有方便的功能,如果其自身的任何关键字失败,它可以自动执行关键字。默认情况下,它使用Capture Page Screenshot关键字,但是在导入库时,可以通过使用Register Keyword在失败时运行关键字或与run_on_failure参数一起更改。可以使用任何导入的库或资源文件中的任何关键字。
可以通过使用特殊值NOTHING或任何被认为是错误的值(请参阅布尔参数)(例如NONE)来禁用运行失败功能。
布尔参数
一些关键字接受作为布尔值true或false处理的参数。如果将这样的参数作为字符串给出,则如果该参数为空或不区分大小写,等于false,no,off,0或none,则将其视为false。不管其他字符串的值如何,都认为它们是正确的,并且使用与Python中相同的规则测试其他参数类型。
Ture 的例子:
Set Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 30: …ersist=True #̲ 字符串通常为true. Se…{RESULTS} persist=yes # 与上述相同.
Set Screenshot Directory
R
E
S
U
L
T
S
p
e
r
s
i
s
t
=
{RESULTS} persist=
RESULTS persist={TRUE} # Python True为true.
Set Screenshot Directory
R
E
S
U
L
T
S
p
e
r
s
i
s
t
=
{RESULTS} persist=
RESULTS persist={42} # 非0的数字为 true.
False 的例子:
Set Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 31: …rsist=False #̲字符串false为false.…{RESULTS} persist=no # 另外,字符串no为false.
Set Screenshot Directory KaTeX parse error: Expected 'EOF', got '#' at position 30: …ersist=NONE #̲ 字符串NONE为false.…{RESULTS} persist=KaTeX parse error: Expected 'EOF', got '#' at position 12: {EMPTY} #̲ 空字符串为false. Se…{RESULTS} persist=KaTeX parse error: Expected 'EOF', got '#' at position 12: {FALSE} #̲ Python False为f…{RESULTS} persist=${NONE} # Python None为false.
请注意,在SeleniumLibrary 3.0之前,所有非空字符串(包括false,no和none)都被视为true。从SeleniumLibrary 4.0开始,字符串0和off被视为false。
WebDriver事件
SeleniumLibrary提供对EventFiringWebDriver的支持。有关更多详细信息,请参阅Selenium和SeleniumLibrary EventFiringWebDriver支持文档。
SeleniumLibrary 4.0中新增了EventFiringWebDriver。
EventFiringWebDriver是Selenium提供的API的侦听器类型。 EventFiringWebDriver允许侦听Selenium API调用,并允许用户在Selenium API方法之前和之后触发事件。 请参阅Selenium EventFiringWebDriver文档,支持哪些Selenium API方法以及EventFiringWebDriver的工作方式。
Selenium EventFiringWebDriver文档
类:
class selenium.webdriver.support.event_firing_webdriver.EventFiringWebDriver(driver, event_listener)[source]
A wrapper around an arbitrary WebDriver instance which supports firing events
Creates a new instance of the EventFiringWebDriver
Args:
driver : A WebDriver instance
event_listener : Instance of a class that subclasses AbstractEventListener and implements it fully or partially
例子:
from selenium.webdriver import Firefox
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(AbstractEventListener):
def before_navigate_to(self, url, driver):
print(“Before navigate to %s” % url)
def after_navigate_to(self, url, driver):
print(“After navigate to %s” % url)
driver = Firefox()
ef_driver = EventFiringWebDriver(driver, MyListener())
ef_driver.get(“http://www.google.co.in/”)
方法:
back()[source]
close()[source]
execute_async_script(script, *args)[source]
execute_script(script, *args)[source]
find_element(by=‘id’, value=None)[source]
find_element_by_class_name(name)[source]
find_element_by_css_selector(css_selector)[source]
find_element_by_id(id_)[source]
find_element_by_link_text(link_text)[source]
find_element_by_name(name)[source]
find_element_by_partial_link_text(link_text)[source]
find_element_by_tag_name(name)[source]
find_element_by_xpath(xpath)[source]
find_elements(by=‘id’, value=None)[source]
find_elements_by_class_name(name)[source]
find_elements_by_css_selector(css_selector)[source]
find_elements_by_id(id_)[source]
find_elements_by_link_text(text)[source]¶
find_elements_by_name(name)[source]
find_elements_by_partial_link_text(link_text)[source]
find_elements_by_tag_name(name)[source]
find_elements_by_xpath(xpath)[source]
forward()[source]
get(url)[source]
quit()[source]
wrapped_driver
Returns the WebDriver instance wrapped by this EventsFiringWebDriver
类:
class selenium.webdriver.support.event_firing_webdriver.EventFiringWebElement(webelement, ef_driver)[source]
” A wrapper around WebElement instance which supports firing events
Creates a new instance of the EventFiringWebElement
方法:
clear()[source]
click()[source]
find_element(by=‘id’, value=None)[source]
find_element_by_class_name(name)[source]
find_element_by_css_selector(css_selector)[source]
find_element_by_id(id_)[source]
find_element_by_link_text(link_text)[source]
find_element_by_name(name)[source]
find_element_by_partial_link_text(link_text)[source]
find_element_by_tag_name(name)[source]
find_element_by_xpath(xpath)[source]
find_elements(by=‘id’, value=None)[source]
find_elements_by_class_name(name)[source]
find_elements_by_css_selector(css_selector)[source]
find_elements_by_id(id_)[source]
find_elements_by_link_text(link_text)[source]
find_elements_by_name(name)[source]
find_elements_by_partial_link_text(link_text)[source]
find_elements_by_tag_name(name)[source]
find_elements_by_xpath(xpath)[source]
send_keys(*value)[source]
wrapped_element
Returns the WebElement wrapped by this EventFiringWebElement instance
通过Selenium EventFiringWebDriver文档我们看可以看到,selenium的webdriver监听器可以在webdriver的实例前和实例后执行一些操作,封装的方法可以通过id、name、css、链接、xpath、tag来识别元素。
线程支持
SeleniumLibrary不是线程安全的。这主要是因为底层的Selenium工具在一个浏览器/驱动程序实例中不是线程安全的。由于Selenium方面的限制,SeleniumLibrary提供的关键字或API不是线程安全的。
插件
SeleniumLibrary提供插件,以作为修改和添加库关键字以及修改某些内部功能的一种方式,而无需创建新的库或修改源代码。有关更多详细信息,请参见插件API文档。
插件API基于SeleniumLibrary 4.0的新版本
引入
我们在引入seleniumlibrary库的时候可以使用的参数列表:
timeout=5.0
implicit_wait=0.0
run_on_failure=Capture Page Screenshot
screenshot_root_directory=None
plugins=None
event_firing_webdriver=None
SeleniumLibrary在引入时可以使用下面的参数设置:
timeout: 与Wait …关键字一起使用的超时的默认值.
implicit_wait: 定位元素时使用的隐式等待的默认值.
run_on_failure: 定位元素时使用的隐式等待的默认值(Capture Page Screenshot是失败时进行截图).
screenshot_root_directory: 创建可能的屏幕截图或EMBED的文件夹的路径。有关EMBED的更多详细信息,请参见设置屏幕快照目录关键字。如果未提供,则使用写入日志文件的目录.
plugins: 允许使用外部Python类扩展SeleniumLibrary.
event_firing_webdriver: 使用EventFiringWebDriver包装selenium类.
作者:马克社区何老师