博主是python小白一枚。
项目是参考白月黑羽的python学习网站做成的。
我参考的网站
http://www.python3.vip/tut/auto/selenium/01/
准备工作:
1、下载并安装谷歌浏览器,查看其版本号,我的版本号是91.0.4472.124。
注意前三位就好了

2、下载对应的驱动程序
下边的链接是浏览器驱动的
https://chromedriver.storage.googleapis.com/index.html
驱动选择对应操作系统的,windows系统选win32的就好
3、驱动程序解压之后,存储路径不能有中文字符
以下为代码实现
from time import sleep
from selenium import webdriver
# 创建 WebDriver 对象,指明使用chrome浏览器驱动
wd = webdriver.Chrome(r'D:\chromedriver_win32\chromedriver.exe')
# 设置最大等待时长为 10秒
wd.implicitly_wait(40)
# 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
wd.get('https://www.huya.com')
mainWindow = wd.current_window_handle
element = wd.find_element_by_class_name("LoginHd--1Jf6S0CCU3DUkJdjVqVn3")
element.click()
wd.switch_to.frame("UDBSdkLgn_iframe")
loginIcon = wd.find_element_by_class_name("input-login")
loginIcon.click()
qqIcon = wd.find_element_by_class_name("qq-icon")
qqIcon.click()
for handle in wd.window_handles:
# 先切换到该窗口
wd.switch_to.window(handle)
# 得到该窗口的标题栏字符串,判断是不是我们要操作的那个窗口
if 'QQ' in wd.title:
# 如果是,那么这时候WebDriver对象就是对应的该该窗口,正好,跳出循环,
wd.switch_to.frame("ptlogin_iframe")
# 993657559是我的QQ,需要填你自己的QQ,
qqSpan = wd.find_element_by_id("img_out_993657559")
qqSpan.click()
break
wd.switch_to.window(mainWindow)
sleep(10)
# 要发送弹幕的直播间
wd.get("https://www.huya.com/24055866")
# 消息栏
messageBox = wd.find_element_by_id("pub_msg_input")
# 发送消息的按钮
messageBut = wd.find_element_by_id("msg_send_bt")
sleep(10)
a=1
while a == 1 :
# 这里使用了从文件读取写好的文字做弹幕
with open("PoetryAdaptation.txt", "r",encoding="utf-8") as f:
for line in f.readlines():
messageBox.send_keys(line)
line = line.strip('\n') # 去掉列表中每一个元素的换行符
messageBut.click()
sleep(30)
pass
版权声明:本文为qq_37081057原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。