Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click on random link in webpage using selenium API for python

How can I click on random link on a given webpage using selenium API for python. ? I'm using python 2.7. Thanks

like image 658
rok Avatar asked Dec 27 '22 02:12

rok


1 Answers

find_elements_by_tagname() will surely work. There is another option also. You can use find_elements_by_partial_link_text where you can pass empty string.

>>> from selenium import webdriver
>>> from random import randint

>>> driver = webdriver.Firefox()
>>> driver.get('http://www.python.org')

>>> links = driver.find_elements_by_partial_link_text('')
>>> l = links[randint(0, len(links)-1)]
>>> l.click()
like image 98
Ansuman Bebarta Avatar answered Apr 30 '23 18:04

Ansuman Bebarta