Condition
crawlipt包含了一些内置的condition,以便你在与网页进行交互可以进行一些逻辑判断。同时你也可以添加自己的condition方法,进行扩展。
内置的condition方法
与action方法不同,condition方法的返回值均为bool类型,你可以在while、if、check中使用它。
presence_of_element_located
presence
xpath: str,
wait: float = 1(最长等待xpath对应元素出现时间)
判断xpath对应单个元素是否在wait时间内出现在dom结构中
presence_of_all_elements_located
presences
xpath: str,
wait: float = 1
判断xpath对应所有元素是否在wait时间内出现在dom结构中
visibility_of_element_located
visibility
xpath: str,
wait: float = 1
判断xpath对应单个元素是否在wait时间内出现在dom结构中,并且宽和高均不为0
invisibility_of_element_located
invisibility
xpath: str,
wait: float = 1
判断xpath对应单个元素是否在wait时间内出现在dom结构中,并且宽和高均为0
frame_to_be_available_and_switch_to_it
None
xpath: str,
wait: float = 1
判断xpath对应的frame是否能在wait时间内被切入
element_to_be_clickable
clickable
xpath: str,
wait: float = 1
判断xpath对应的单个元素是否能在wait时间内被点击
element_located_to_be_selected
selected
xpath: str,
wait: float = 1
判断xpath对应的单个元素是否能在wait时间内被选择
text_to_be_present_in_element
None
xpath: str,
text: str,
wait: float = 1
判断xpath对应的单个元素是否能在wait时间内出现text文本内容
text_to_be_present_in_element_value
None
xpath: str,
value: str,
wait: float = 1
判断xpath对应的单个元素的value中是否能在wait时间内出现value文本内容
alert_is_present
None
wait: float = 1
判断是否有alert弹出
fail_script关键词
在condition中如果含有fail_script关键词,则在condition失败后会执行该脚本
{
"check": {
"condition": "presence",
"xpath": "//*[@id=\"main-metro\"]/ul/li[3]/a[3]",
"fail_script": [{
"method": "log",
"msg": "[fail] 登录失败"
}]
}
}
逻辑取反
使用__not-{your condition}__指令可以对condition结果进行取反
{
"check": {
"condition": "__not-presence__",
"xpath": "//*[@id=\"main-metro\"]/ul/li[3]/a[3]",
"fail_script": [{
"method": "log",
"msg": "[fail] 登录失败"
}]
}
}
添加你自己的condition
在你添加自己的action方法前,建议你先学习一下selenium的基本使用
请参考下面的示例
import crawlipt as cpt
from selenium.webdriver.remote.webdriver import WebDriver
"""
(1)必须为一个可调用的函数或者类内的静态方法
(2)必须使用check注解,来排除driver的语法检查,否则在语法检查阶段会抛出异常
(3)所有的参数必须注明类型,否则无法通过语法检查
(4)所有参数必须为python的基础类型
(5)必须注明函数返回值,且返回值类型必须为bool类型
(6)driver是固定变量,即必须包含(driver: WebDriver)这个参数
"""
@cpt.check(exclude="driver")
def myConditon(driver: WebDriver, **args) -> bool:
"""
your doc
:param driver: selenium webdriver
:param **args: your args
"""
# write your code
if ...:
return True
else:
return False
# 按照如下方式添加脚本
cpt.Script.add_condition(myConditon)
最后更新于