@@ -1213,32 +1213,35 @@ def set(
12131213 item_loc = self ._loc_events .locator ("> .item" ).nth (i )
12141214 item_data_value = item_loc .get_attribute ("data-value" )
12151215
1216- # If the item has no data-value, remove it
1217- if item_data_value is None :
1216+ def delete_item () -> None :
1217+ """
1218+ Deletes the item by clicking on it and pressing the Delete key.
1219+ """
12181220 item_loc .click ()
12191221 self .page .keyboard .press ("Delete" )
1222+
1223+ # If the item has no data-value, remove it
1224+ if item_data_value is None :
1225+ delete_item ()
12201226 continue
12211227
12221228 # If there are more items than selected, we need to remove the extra ones
12231229 if i >= len (selected ):
1224- # If we have more items than selected, remove the extra ones
1225- item_loc .click ()
1226- self .page .keyboard .press ("Delete" )
1230+ delete_item ()
12271231 continue
12281232
12291233 selected_data_value = selected [i ]
12301234
1231- # If the item is not selected, remove it
1235+ # If the item is not the next ` selected` value , remove it
12321236 if item_data_value != selected_data_value :
1233- item_loc .click ()
1234- self .page .keyboard .press ("Delete" )
1237+ delete_item ()
12351238 continue
12361239
1237- # The item is the next selected value
1240+ # The item is the next ` selected` value
12381241 # Increment the index!
12391242 i += 1
12401243
1241- # If we have less items than selected, add the remaining items
1244+ # Add the remaining items
12421245 if i < len (selected ):
12431246 for data_value in selected [i :]:
12441247 # Click on the item in the dropdown to select it
0 commit comments