from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import random
# 需求 在百度中搜索内容并随机点击显示出的搜索词条
d = webdriver.Chrome()
# 搜索网址
d.get('https://www.baidu.com/')
# 在搜索框中写入搜索内容
d.find_element_by_id('kw').send_keys('火影忍者')
# 点击搜索按钮
d.find_element_by_id('su').click()
sleep(3)
# 利用xpath选到显示出来的词条
list = d.find_elements_by_xpath('//h3[@]/a')
print(len(list))
sleep(5)
# 设置随机数,并进行点击
i = random.randint(0, len(list))
list[i].click()
sleep(5)
d.quit()
(水平有限,只求实现,大佬们看着一乐吧)



