Random Element Selection from Selector

  • Share this:

Code introduction


This function randomly selects an element from the specified selector. If no elements match, it returns None.


Technology Stack : Selenium, Python

Code Type : Selenium Function

Code Difficulty : Intermediate


                
                    
def random_select_element(driver, by, value):
    elements = driver.find_elements(by, value)
    return elements[random.randint(0, len(elements) - 1)] if elements else None