简体   繁体   English

Python Selenium 随机 xpath 单击

[英]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.我希望我的代码从网页中选择一个随机的 xpath,并在每次运行我的代码时点击它,在 Python 中使用 Selenium。

Can you help me with it?你能帮我解决吗?

Thank you.谢谢你。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM