鱼C论坛

 找回密码
 立即注册
查看: 2545|回复: 3

[已解决]关于虎牙账号密码自动登陆的问题selenium

[复制链接]
发表于 2023-3-4 13:54:54 | 显示全部楼层 |阅读模式
30鱼币
本帖最后由 费小牛 于 2023-3-4 13:58 编辑

运行代码跳转到扫码登陆界面却无法转到账户密码界面,手动转入界面也不行账户密码也不能自动输入,试了好久,恳请大佬帮忙,登陆后自动发弹幕

  1. from random import choice
  2. from time import sleep
  3. from selenium import webdriver
  4. from selenium.webdriver import ActionChains
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.chrome.service import Service

  7. def HuyaDanmu(address,qq,roomNum,ua):
  8.     options = webdriver.ChromeOptions()            # 定义Chrome配置参数
  9.     options.add_argument('--ignore-certificate-errors')           # 忽略https证书错误
  10.     options.add_argument('user-agent=' + ua)                      # 设置user-agent,取消浏览器被识别出来

  11.     s = Service(address)                                            # 加载Chrome服务
  12.     wd = webdriver.Chrome(service=s, options=options)                 # 创建浏览器实例
  13.     wd.implicitly_wait(20)                                          # 让浏览器实例隐式等待3秒,方便浏览器加载完成后就可以获取页面的元素
  14.     # 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
  15.     wd.get('https://www.huya.com')
  16.     #点击登陆出现扫码登陆
  17.     wd.find_element(By.LINK_TEXT, "登录").click()
  18.     #账号密码在记事本中,第一行账号,第二行密码
  19.     with open(r"G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\HY账号密码", "r", encoding="utf-8") as g:
  20.         hypassword = g.readlines()

  21.     # 由于登录按钮是在iframe上,所以把定位器切换到iframe上
  22.     iframe = wd.find_element(By.ID, 'id="UDBSdkLgn_iframe"')
  23.     wd.switch_to.frame(iframe)
  24.     wd.find_element(By.XPATH, '//*[@id="quick-login-section"]/div[3]/i').click()
  25.     wd.find_element(By.XPATH,'//*[@id="account-login-form"]/div[1]/input').send_keys(hypassword[0].strip("\n"))
  26.     wd.find_element('//div[@class="udb-input-item"]//input[@placeholder="密码"]').send_keys(hypassword[1].strip("\n"))
  27.     wd.find_element(By.ID,"login-btn").click()

  28.     sleep(10)

  29.     roomHref = "https://www.huya.com/" + roomNum
  30.     wd.get(roomHref)

  31.     f = open("danmu.txt", "r",encoding="UTF-8")  # 设置文件对象
  32.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  33.     f.close()
  34.     while 1 == 1:
  35.         messageBox = wd.find_element(By.XPATH, '//*[@id="pub_msg_input"]')       # 找到页面上的输入框,用于发送弹幕
  36.         messageBut = wd.find_element(By.XPATH, '//*[@id="msg_send_bt"]')        # 找到发送弹幕按钮
  37.         danmu = choice(lines).strip('\n')                            # 随机从文件中读取一个弹幕
  38.         messageBox.send_keys(danmu)                     # 在输入框中输入弹幕
  39.         messageBut.click()              # 点击发送弹幕
  40.         sleep(100)

  41. if __name__ == '__main__':
  42.     f = open("settings.txt", "r",encoding='utf-8')  # 设置文件对象
  43.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  44.     address="G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\chromedriver.exe"
  45.     ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  46.     f.close()

  47.     roomNum = "*****"       #虎牙房间号
  48.     HuyaDanmu(address,qq,roomNum,ua)
  49.     input("输入任意键结束")
复制代码
最佳答案
2023-3-4 13:54:55
本帖最后由 isdkz 于 2023-3-4 17:50 编辑

先在浏览器上模拟切到账号登录一遍嘛,

看我在最下面加的调试代码来做参考对你的代码进行修改

  1. from random import choice
  2. from time import sleep
  3. from selenium import webdriver
  4. from selenium.webdriver import ActionChains
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.chrome.service import Service

  7. def HuyaDanmu(address,qq,roomNum,ua):
  8.     options = webdriver.ChromeOptions()            # 定义Chrome配置参数
  9.     options.add_argument('--ignore-certificate-errors')           # 忽略https证书错误
  10.     options.add_argument('user-agent=' + ua)                      # 设置user-agent,取消浏览器被识别出来

  11.     s = Service(address)                                            # 加载Chrome服务
  12.     wd = webdriver.Chrome(service=s, options=options)                 # 创建浏览器实例
  13.     wd.implicitly_wait(20)                                          # 让浏览器实例隐式等待3秒,方便浏览器加载完成后就可以获取页面的元素
  14.     # 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
  15.     wd.get('https://www.huya.com')
  16.     #点击登陆出现扫码登陆
  17.     wd.find_element(By.LINK_TEXT, "登录").click()
  18.     #账号密码在记事本中,第一行账号,第二行密码
  19.     with open(r"G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\HY账号密码", "r", encoding="utf-8") as g:
  20.         hypassword = g.readlines()

  21.     # 由于登录按钮是在iframe上,所以把定位器切换到iframe上
  22.     iframe = wd.find_element(By.ID, 'id="UDBSdkLgn_iframe"')
  23.     wd.switch_to.frame(iframe)
  24.     wd.find_element(By.XPATH, '//*[@id="quick-login-section"]/div[3]/i').click()
  25.     wd.find_element(By.XPATH,'//*[@id="account-login-form"]/div[1]/input').send_keys(hypassword[0].strip("\n"))
  26.     wd.find_element('//div[@class="udb-input-item"]//input[@placeholder="密码"]').send_keys(hypassword[1].strip("\n"))
  27.     wd.find_element(By.ID,"login-btn").click()

  28.     sleep(10)

  29.     roomHref = "https://www.huya.com/" + roomNum
  30.     wd.get(roomHref)

  31.     f = open("danmu.txt", "r",encoding="UTF-8")  # 设置文件对象
  32.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  33.     f.close()
  34.     while 1 == 1:
  35.         messageBox = wd.find_element(By.XPATH, '//*[@id="pub_msg_input"]')       # 找到页面上的输入框,用于发送弹幕
  36.         messageBut = wd.find_element(By.XPATH, '//*[@id="msg_send_bt"]')        # 找到发送弹幕按钮
  37.         danmu = choice(lines).strip('\n')                            # 随机从文件中读取一个弹幕
  38.         messageBox.send_keys(danmu)                     # 在输入框中输入弹幕
  39.         messageBut.click()              # 点击发送弹幕
  40.         sleep(100)

  41. '''
  42.     f = open("settings.txt", "r",encoding='utf-8')  # 设置文件对象
  43.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  44.     address="G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\chromedriver.exe"
  45.     ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  46.     f.close()

  47.     roomNum = "*****"       #虎牙房间号
  48.     HuyaDanmu(address,qq,roomNum,ua)
  49.     input("输入任意键结束")
  50. '''





  51. # 看我这一段代码来参考,这是我在自己电脑上调试出可用的
  52. ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  53. options = webdriver.ChromeOptions()
  54. options.add_argument('--ignore-certificate-errors')
  55. options.add_argument('user-agent=' + ua)
  56. address="chromedriver.exe"
  57. s = Service(address)
  58. wd = webdriver.Chrome(service=s, options=options)   
  59. wd.implicitly_wait(20)     
  60. wd.get('https://www.huya.com')
  61. wd.find_element(By.LINK_TEXT, "点我注册").click()
  62. wd.switch_to.frame("UDBSdkLgn_iframe")
  63. wd.find_element(By.CLASS_NAME, 'returnlogin-btn').click()
复制代码

最佳答案

查看完整内容

先在浏览器上模拟切到账号登录一遍嘛, 看我在最下面加的调试代码来做参考对你的代码进行修改
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-4 13:54:55 | 显示全部楼层    本楼为最佳答案   
本帖最后由 isdkz 于 2023-3-4 17:50 编辑

先在浏览器上模拟切到账号登录一遍嘛,

看我在最下面加的调试代码来做参考对你的代码进行修改

  1. from random import choice
  2. from time import sleep
  3. from selenium import webdriver
  4. from selenium.webdriver import ActionChains
  5. from selenium.webdriver.common.by import By
  6. from selenium.webdriver.chrome.service import Service

  7. def HuyaDanmu(address,qq,roomNum,ua):
  8.     options = webdriver.ChromeOptions()            # 定义Chrome配置参数
  9.     options.add_argument('--ignore-certificate-errors')           # 忽略https证书错误
  10.     options.add_argument('user-agent=' + ua)                      # 设置user-agent,取消浏览器被识别出来

  11.     s = Service(address)                                            # 加载Chrome服务
  12.     wd = webdriver.Chrome(service=s, options=options)                 # 创建浏览器实例
  13.     wd.implicitly_wait(20)                                          # 让浏览器实例隐式等待3秒,方便浏览器加载完成后就可以获取页面的元素
  14.     # 调用WebDriver 对象的get方法 可以让浏览器打开指定网址
  15.     wd.get('https://www.huya.com')
  16.     #点击登陆出现扫码登陆
  17.     wd.find_element(By.LINK_TEXT, "登录").click()
  18.     #账号密码在记事本中,第一行账号,第二行密码
  19.     with open(r"G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\HY账号密码", "r", encoding="utf-8") as g:
  20.         hypassword = g.readlines()

  21.     # 由于登录按钮是在iframe上,所以把定位器切换到iframe上
  22.     iframe = wd.find_element(By.ID, 'id="UDBSdkLgn_iframe"')
  23.     wd.switch_to.frame(iframe)
  24.     wd.find_element(By.XPATH, '//*[@id="quick-login-section"]/div[3]/i').click()
  25.     wd.find_element(By.XPATH,'//*[@id="account-login-form"]/div[1]/input').send_keys(hypassword[0].strip("\n"))
  26.     wd.find_element('//div[@class="udb-input-item"]//input[@placeholder="密码"]').send_keys(hypassword[1].strip("\n"))
  27.     wd.find_element(By.ID,"login-btn").click()

  28.     sleep(10)

  29.     roomHref = "https://www.huya.com/" + roomNum
  30.     wd.get(roomHref)

  31.     f = open("danmu.txt", "r",encoding="UTF-8")  # 设置文件对象
  32.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  33.     f.close()
  34.     while 1 == 1:
  35.         messageBox = wd.find_element(By.XPATH, '//*[@id="pub_msg_input"]')       # 找到页面上的输入框,用于发送弹幕
  36.         messageBut = wd.find_element(By.XPATH, '//*[@id="msg_send_bt"]')        # 找到发送弹幕按钮
  37.         danmu = choice(lines).strip('\n')                            # 随机从文件中读取一个弹幕
  38.         messageBox.send_keys(danmu)                     # 在输入框中输入弹幕
  39.         messageBut.click()              # 点击发送弹幕
  40.         sleep(100)

  41. '''
  42.     f = open("settings.txt", "r",encoding='utf-8')  # 设置文件对象
  43.     lines = f.readlines()  # 将txt文件的所有内容读入到字符串str中
  44.     address="G:\TDDOWNLOAD\qq自动化登录虎牙\LoginHuyaByQ\chromedriver.exe"
  45.     ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  46.     f.close()

  47.     roomNum = "*****"       #虎牙房间号
  48.     HuyaDanmu(address,qq,roomNum,ua)
  49.     input("输入任意键结束")
  50. '''





  51. # 看我这一段代码来参考,这是我在自己电脑上调试出可用的
  52. ua="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
  53. options = webdriver.ChromeOptions()
  54. options.add_argument('--ignore-certificate-errors')
  55. options.add_argument('user-agent=' + ua)
  56. address="chromedriver.exe"
  57. s = Service(address)
  58. wd = webdriver.Chrome(service=s, options=options)   
  59. wd.implicitly_wait(20)     
  60. wd.get('https://www.huya.com')
  61. wd.find_element(By.LINK_TEXT, "点我注册").click()
  62. wd.switch_to.frame("UDBSdkLgn_iframe")
  63. wd.find_element(By.CLASS_NAME, 'returnlogin-btn').click()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2023-3-8 18:58:01 | 显示全部楼层
isdkz 发表于 2023-3-4 13:54
先在浏览器上模拟切到账号登录一遍嘛,

看我在最下面加的调试代码来做参考对你的代码进行修改

牛啊 一下就解决了,selenium的好教程有没有推荐呀,我想学习一下 好多不懂,我是东拼西凑型,好难受呀
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-3-8 19:32:01 | 显示全部楼层
费小牛 发表于 2023-3-8 18:58
牛啊 一下就解决了,selenium的好教程有没有推荐呀,我想学习一下 好多不懂,我是东拼西凑型,好难受呀

我看过的比较好的 selenium 教程是极客时间的,不过极客时间的教程需要收费

https://time.geekbang.org/course/intro/100055601?tab=catalog
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 21:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表