Python Selenium Random xpath click

834 views Asked by At

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.

1

There are 1 answers

0
Sushil On BEST ANSWER

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