|
30鱼币
本帖最后由 费小牛 于 2023-3-4 13:58 编辑
运行代码跳转到扫码登陆界面却无法转到账户密码界面,手动转入界面也不行账户密码也不能自动输入,试了好久,恳请大佬帮忙,登陆后自动发弹幕
- from random import choice
- from time import sleep
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.by import By
- from selenium.webdriver.chrome.service import Service
- def HuyaDanmu(address,qq,roomNum,ua):
- options = webdriver.ChromeOptions() # 定义Chrome配置参数
- options.add_argument('--ignore-certificate-errors') # 忽略https证书错误
- options.add_argument('user-agent=' + ua) # 设置user-agent,取消浏览器被识别出来
- s = Service(address) # 加载Chrome服务
- wd = webdriver.Chrome(service=s, options=options) # 创建浏览器实例
- wd.implicitly_wait(20) # 让浏览器实例隐式等待3秒,方便浏览器加载完成后就可以获取页面的元素
- # 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
- wd.get('https://www.huya.com')
- #点击登陆出现扫码登陆
- wd.find_element(By.LINK_TEXT, "登录").click()
- #账号密码在记事本中,第一行账号,第二行密码
- with open(r"G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\HY账号密码", "r", encoding="utf-8") as g:
- hypassword = g.readlines()
- # 由于登录按钮是在iframe上,所以把定位器切换到iframe上
- iframe = wd.find_element(By.ID, 'id="UDBSdkLgn_iframe"')
- wd.switch_to.frame(iframe)
- wd.find_element(By.XPATH, '//*[@id="quick-login-section"]/div[3]/i').click()
- wd.find_element(By.XPATH,'//*[@id="account-login-form"]/div[1]/input').send_keys(hypassword[0].strip("\n"))
- wd.find_element('//div[@class="udb-input-item"]//input[@placeholder="密码"]').send_keys(hypassword[1].strip("\n"))
- wd.find_element(By.ID,"login-btn").click()
- sleep(10)
- roomHref = "https://www.huya.com/" + roomNum
- wd.get(roomHref)
- f = open("danmu.txt", "r",encoding="UTF-8") # 设置文件对象
- lines = f.readlines() # 将txt文件的所有内容读入到字符串str中
- f.close()
- while 1 == 1:
- messageBox = wd.find_element(By.XPATH, '//*[@id="pub_msg_input"]') # 找到页面上的输入框,用于发送弹幕
- messageBut = wd.find_element(By.XPATH, '//*[@id="msg_send_bt"]') # 找到发送弹幕按钮
- danmu = choice(lines).strip('\n') # 随机从文件中读取一个弹幕
- messageBox.send_keys(danmu) # 在输入框中输入弹幕
- messageBut.click() # 点击发送弹幕
- sleep(100)
- if __name__ == '__main__':
- f = open("settings.txt", "r",encoding='utf-8') # 设置文件对象
- lines = f.readlines() # 将txt文件的所有内容读入到字符串str中
- address="G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\chromedriver.exe"
- ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
- f.close()
- roomNum = "*****" #虎牙房间号
- HuyaDanmu(address,qq,roomNum,ua)
- input("输入任意键结束")
复制代码
本帖最后由 isdkz 于 2023-3-4 17:50 编辑
先在浏览器上模拟切到账号登录一遍嘛,
看我在最下面加的调试代码来做参考对你的代码进行修改
- from random import choice
- from time import sleep
- from selenium import webdriver
- from selenium.webdriver import ActionChains
- from selenium.webdriver.common.by import By
- from selenium.webdriver.chrome.service import Service
- def HuyaDanmu(address,qq,roomNum,ua):
- options = webdriver.ChromeOptions() # 定义Chrome配置参数
- options.add_argument('--ignore-certificate-errors') # 忽略https证书错误
- options.add_argument('user-agent=' + ua) # 设置user-agent,取消浏览器被识别出来
- s = Service(address) # 加载Chrome服务
- wd = webdriver.Chrome(service=s, options=options) # 创建浏览器实例
- wd.implicitly_wait(20) # 让浏览器实例隐式等待3秒,方便浏览器加载完成后就可以获取页面的元素
- # 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
- wd.get('https://www.huya.com')
- #点击登陆出现扫码登陆
- wd.find_element(By.LINK_TEXT, "登录").click()
- #账号密码在记事本中,第一行账号,第二行密码
- with open(r"G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\HY账号密码", "r", encoding="utf-8") as g:
- hypassword = g.readlines()
- # 由于登录按钮是在iframe上,所以把定位器切换到iframe上
- iframe = wd.find_element(By.ID, 'id="UDBSdkLgn_iframe"')
- wd.switch_to.frame(iframe)
- wd.find_element(By.XPATH, '//*[@id="quick-login-section"]/div[3]/i').click()
- wd.find_element(By.XPATH,'//*[@id="account-login-form"]/div[1]/input').send_keys(hypassword[0].strip("\n"))
- wd.find_element('//div[@class="udb-input-item"]//input[@placeholder="密码"]').send_keys(hypassword[1].strip("\n"))
- wd.find_element(By.ID,"login-btn").click()
- sleep(10)
- roomHref = "https://www.huya.com/" + roomNum
- wd.get(roomHref)
- f = open("danmu.txt", "r",encoding="UTF-8") # 设置文件对象
- lines = f.readlines() # 将txt文件的所有内容读入到字符串str中
- f.close()
- while 1 == 1:
- messageBox = wd.find_element(By.XPATH, '//*[@id="pub_msg_input"]') # 找到页面上的输入框,用于发送弹幕
- messageBut = wd.find_element(By.XPATH, '//*[@id="msg_send_bt"]') # 找到发送弹幕按钮
- danmu = choice(lines).strip('\n') # 随机从文件中读取一个弹幕
- messageBox.send_keys(danmu) # 在输入框中输入弹幕
- messageBut.click() # 点击发送弹幕
- sleep(100)
- '''
- f = open("settings.txt", "r",encoding='utf-8') # 设置文件对象
- lines = f.readlines() # 将txt文件的所有内容读入到字符串str中
- address="G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\chromedriver.exe"
- ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
- f.close()
- roomNum = "*****" #虎牙房间号
- HuyaDanmu(address,qq,roomNum,ua)
- input("输入任意键结束")
- '''
- # 看我这一段代码来参考,这是我在自己电脑上调试出可用的
- ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
- options = webdriver.ChromeOptions()
- options.add_argument('--ignore-certificate-errors')
- options.add_argument('user-agent=' + ua)
- address="chromedriver.exe"
- s = Service(address)
- wd = webdriver.Chrome(service=s, options=options)
- wd.implicitly_wait(20)
- wd.get('https://www.huya.com')
- wd.find_element(By.LINK_TEXT, "点我注册").click()
- wd.switch_to.frame("UDBSdkLgn_iframe")
- wd.find_element(By.CLASS_NAME, 'returnlogin-btn').click()
复制代码
|
最佳答案
查看完整内容
先在浏览器上模拟切到账号登录一遍嘛,
看我在最下面加的调试代码来做参考对你的代码进行修改
|