Condition

Crawlipt includes some built-in conditions so that you can make logical judgments when interacting with web pages. At the same time, you can also add your own condition method for extension.

Built-in condition method

Unlike the action method, the return value of the condition method is of type bool, which you can use in while, if, and check.

methodaliasparmsnotes

presence_of_element_located

presence

xpath: str,

wait: float = 1(The longest waiting time for the appearance of the corresponding element in xpath)

Determine whether a single element corresponding to xpath appears in the dom structure within the wait time

presence_of_all_elements_located

presences

xpath: str,

wait: float = 1

Determine whether all elements corresponding to xpath appear in the dom structure within the wait time

visibility_of_element_located

visibility

xpath: str,

wait: float = 1

Determine whether a single element corresponding to xpath appears in the dom structure within the wait time, and whether the width and height are not 0

invisibility_of_element_located

invisibility

xpath: str,

wait: float = 1

Determine whether a single element corresponding to xpath appears in the dom structure within the wait time, and whether its width and height are both 0

frame_to_be_available_and_switch_to_it

None

xpath: str,

wait: float = 1

Determine whether the frame corresponding to xpath can be cut in within the wait time

element_to_be_clickable

clickable

xpath: str,

wait: float = 1

Determine whether a single element corresponding to xpath can be clicked within the wait time

element_located_to_be_selected

selected

xpath: str,

wait: float = 1

Determine whether a single element corresponding to xpath can be selected within the wait time

text_to_be_present_in_element

None

xpath: str,

text: str,

wait: float = 1

Determine whether a single element corresponding to xpath can contain text content within the wait time

text_to_be_present_in_element_value

None

xpath: str,

value: str,

wait: float = 1

Determine whether the value text content can appear within the wait time in the value of a single element corresponding to xpath

alert_is_present

None

wait: float = 1

Determine if there is an alert pop-up

'fail_script' keyword

If the condition contains the keyword 'fail script', the script will be executed after the condition fails

{
    "check": {
        "condition": "presence",
        "xpath": "//*[@id=\"main-metro\"]/ul/li[3]/a[3]",
        "fail_script": [{
            "method": "log",
            "msg": "[fail] login failed"
        }]
    }
}

Logical inversion

The use of the _not-{your condition}__ instruction can negate the condition result

{
    "check": {
        "condition": "__not-presence__",
        "xpath": "//*[@id=\"main-metro\"]/ul/li[3]/a[3]",
        "fail_script": [{
            "method": "log",
            "msg": "[fail] login failed"
        }]
    }
}

Add your own conditions

Before adding your own action method, it is recommended that you first learn the basic usage of selenium

Please refer to the following example

import crawlipt as cpt
from selenium.webdriver.remote.webdriver import WebDriver
"""
(1) Must be a callable function or static method within a class
(2) The check annotation must be used to exclude the syntax check of the driver, otherwise an exception will be thrown during the syntax check phase
(3) All parameters must indicate the type, otherwise they cannot pass the syntax check
(4) All parameters must be of the underlying type in Python
(5) The function return value must be specified, and the return value type must be bool type
(6) Driver is a fixed variable, which means it must include the parameter (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

# Add the script as follows
cpt.Script.add_condition(myConditon)

Last updated