In the mouse operation, the ActionChains class provided by selenium is used.
Here is a brief introduction to the execution principle of ActionChains: when the method of ActionChains is called, it will not be executed immediately, but will be stored in a list in order. When the perform() method is called, the methods stored in the list are executed sequentially.
1. ActionChains class code view
- In the code, hold down the ctrl key and click ActionChains to open the corresponding code file
- Open the Structure, you can see all the methods in the ActionChains class in the structure
2. The principle of delayed call
1) Initialize an empty list actions in the __innit() function;
2) Add the name of the operation method to the actions list in each operation method;
3) For loop the actions list in the perform method, call the function action(
) The reason to add .perform(): action.double_click(el).perform()
3. Practice
Write a simple code here to further explain how this delayed call is implemented
# 定义两个操作方法
def click():
print("单击")
def double_click():
print("双击")
# 第一步:定义一个空列表,用来存方法名
actions = []
# 第二步:将操作方法添加到 actions列表
def add(func):
actions.append(func)
# 第三步:在 perform 方法内 for循环actions列表,依次执行存放的方法
def perform():
for action in actions:
action()
# 第四步:只有调用 perform()时,才会真正去执行被添加进来的方法
add(click)
add(double_click)
print(actions)
perform()
Output result:
[<function click at 0x000001BDAE822F70>, <function double_click at 0x000001BDAE824430>]
单击
双击