@@ -1251,26 +1251,40 @@ def wait_for_attribute_not_present(
1251
1251
timeout_exception (Exception , message )
1252
1252
1253
1253
1254
- def find_visible_elements (driver , selector , by = "css selector" ):
1254
+ def find_visible_elements (driver , selector , by = "css selector" , limit = 0 ):
1255
1255
"""
1256
1256
Finds all WebElements that match a selector and are visible.
1257
- Similar to webdriver.find_elements.
1257
+ Similar to webdriver.find_elements().
1258
+ If "limit" is set and > 0, will only return that many elements.
1258
1259
@Params
1259
1260
driver - the webdriver object (required)
1260
1261
selector - the locator for identifying the page element (required)
1261
1262
by - the type of selector being used (Default: "css selector")
1263
+ limit - the maximum number of elements to return if > 0.
1262
1264
"""
1263
1265
elements = driver .find_elements (by = by , value = selector )
1266
+ if limit and limit > 0 and len (elements ) > limit :
1267
+ elements = elements [:limit ]
1264
1268
try :
1265
1269
v_elems = [element for element in elements if element .is_displayed ()]
1266
1270
return v_elems
1267
1271
except (StaleElementReferenceException , ElementNotInteractableException ):
1268
1272
time .sleep (0.1 )
1269
1273
elements = driver .find_elements (by = by , value = selector )
1274
+ extra_elements = []
1275
+ if limit and limit > 0 and len (elements ) > limit :
1276
+ elements = elements [:limit ]
1277
+ extra_elements = elements [limit :]
1270
1278
v_elems = []
1271
1279
for element in elements :
1272
1280
if element .is_displayed ():
1273
1281
v_elems .append (element )
1282
+ if extra_elements and limit and len (v_elems ) < limit :
1283
+ for element in extra_elements :
1284
+ if element .is_displayed ():
1285
+ v_elems .append (element )
1286
+ if len (v_elems ) >= limit :
1287
+ break
1274
1288
return v_elems
1275
1289
1276
1290
0 commit comments