Search code examples
pythonfunctionseleniumautomatione2e

Python Selenium Random xpath click


I want my code to pick a random xpath from the web page and click on it every time i run my code, in Python with Selenium.

Can you help me with it?

Thank you.


Solution

  • This should help you:

    from selenium import webdriver
    import random
    
    driver = webdriver.Chrome()
    
    driver.get('url')
    
    elements = driver.find_elements_by_xpath('//*[@id]') #Finds all elements in the page
    
    element = random.choice(elements) #Selects a random element from the list of elements
    
    element.click() #Clicks on the selected element