Use the selenium module in python to realize the automatic login of NetEase Cloud Music QQ number

One: The introduction of the problem

Computers are omnipotent (don't spray me, personal opinion), so we can use computers to help us when dealing with some problems.

Two: Today we will introduce: using the selenium module in python to realize the automatic login of NetEase Cloud Music QQ number

Three: the code is as follows


# 导入时间模块
import time
# 导入webdriver模块
from selenium import webdriver
# 导入By模块
from selenium.webdriver.common.by import By
# 实例化浏览器对象
driver=webdriver.Chrome()
# 进行浏览器的自动化
driver.get('https://music.163.com/')
# 隐式等待10秒钟(如果网页渲染不需要十秒钟就会提前结束)
driver.implicitly_wait(10)
# 最大化浏览器
driver.maximize_window()
# 点击登录
driver.find_element(By.CSS_SELECTOR,'.link.s-fc3').click()
# 强制睡眠2秒钟,有跳转的都需要等待浏览器的渲染,不然浏览器的响应速度跟不上代码的响应的时间(下面的都是一样的道理)
time.sleep(2)
# 点击其他方式登录
driver.find_element(By.CSS_SELECTOR,'div._1a7hecWJ > div > div > div > a').click()
time.sleep(2)
# 点击同意框
driver.find_element(By.CSS_SELECTOR,'#j-official-terms').click()
# 点击QQ登录
driver.find_element(By.CSS_SELECTOR,'div._3x8w3YCi > ul > li:nth-child(2) > a').click()
time.sleep(2)
# 获取当前浏览器所有的页面句柄
windows = driver.window_handles
# print(windows)  查看当前的网页有多少(可能我说的也不正确啊,可以不用管)
# 跳转到第二个页面,里面嵌套了网页了,不跳转的话,selenium访问不到
driver.switch_to.window(windows[1])
# 目标是嵌套元素,使用对应的函数,跳转到嵌套的网页
driver.switch_to.frame('ptlogin_iframe')
# 点击密码登录
driver.find_element(By.CSS_SELECTOR, '#switcher_plogin').click()
time.sleep(3)
# 获取当前浏览器所有的页面句柄
windows = driver.window_handles
# 跳转到第二个页面,里面嵌套了网页了,不跳转的话,selenium访问不到
driver.switch_to.window(windows[1])
# 跳转到第二个页面,里面嵌套了网页了,不跳转的话,selenium访问不到
driver.switch_to.frame(0)
# 输入你的QQ号
driver.find_element(By.CSS_SELECTOR, '#u').send_keys('QQ号')
time.sleep(2)
# 输入你的密码
driver.find_element(By.CSS_SELECTOR, '#p').send_keys('密码')
time.sleep(2)
# 点击登录
driver.find_element(By.CSS_SELECTOR, '#login_button').click()
time.sleep(3)
# 阻塞一下
input()
# 退出浏览器
driver.quit()


Four: Attention to the problem

1. The environment of the code needs to be configured by yourself, not everything can run

① Configure the .exe file, which must be used when using selenium. Here I use the chrome browser. You can also choose the file that suits you according to your browser and version

insert image description here

②Link address

③ In your browser, find the three dots in the upper right corner (click) and click Help, then click About to view the version number of the browser, here is the version 113.0.5672.63, the version number does not need to be very precise, the previous It's fine if it's right,

insert image description here
insert image description here

④ Note that this file should be placed in the same path as the .py file

2 Write down your own QQ number and password, I will not tell you mine, hahaha, I hope you can communicate with me more, thank you big brother...

Guess you like

Origin blog.csdn.net/m0_74459049/article/details/130548504