python - 使用 selenium API for python 单击网页中的随机链接

标签 python selenium random hyperlink html

如何使用适用于 python 的 selenium API 单击给定网页上的随机链接。 ? 我正在使用 python 2.7。 谢谢

最佳答案

find_elements_by_tagname() 肯定会起作用。还有另一种选择。您可以使用 find_elements_by_partial_link_text 传递空字符串。

>>> 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()

关于python - 使用 selenium API for python 单击网页中的随机链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16980284/

相关文章:

selenium - element.isDisplayed() 和 wait.until(ExpectedConditions.visibilityOf(element)) 之间的区别;

python - 如何在Python中使用random.sample()后获取剩余的样本?

python - 使用spacy在python中使用ML方法(NER)提取日期

python - OpenCV-Python : How to detect a hotspot in thermal image?

python - 将数据帧转换为多索引数据帧

javascript - 为什么 Google Chrome 浏览器的 Math.random 数字生成器不是 *that* 随机数?

android - 如何创建播放列表

python - 'tf.get_default_session()'的用途

c# - Azure网站本地调用WebException

java - 当我在后台运行 Selenium 脚本时,无法最大化 Selenium 中的浏览器