python3.8 +selenium 4.8.0 完成自动登陆

# -*-coding: utf-8 -*-
# @Time    : 2022/9/22 21:26
# @Author  : ck
# @File    : login.py
# @Software: PyCharm
# selenium 4.8.0版本
# python 3.8
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
import time

if __name__ == '__main__':
    driver = None
    try:
        # 实例化一个启动参数对象
        chrome_options = Options()
        # 加载启动项页面全屏效果,相当于F11。
        # chrome_options.add_argument("--kiosk")
        # 禁止谷歌弹出正在被自动化软件控制消息
        # chrome_options.add_experimental_option("excludeSwitches", ['enable-automation'])
        # # 无痕模式
        chrome_options.add_argument('–-incognito')
        # 谷歌浏览器驱动地址
        chrome_path = r'E:\work\googleDriver\chromedriver_win32\chromedriver.exe'
        # 打开浏览器
        driver = webdriver.Chrome(chrome_path)
        driver.set_window_size(1920, 1080)
        # 延迟2秒
        time.sleep(2)
        # 访问https://passport.csdn.net/login?code=applets登陆网址
        driver.get(str('https://passport.csdn.net/login?code=applets'))
        time.sleep(2)
        # 点击密码登陆
        # print(driver.find_element(By.XPATH, "//*[text()='密码登录']").text)
        driver.find_element(By.XPATH, "//*[text()='密码登录']").click()
        time.sleep(2)
        # 输入用户名密码
        driver.find_element(By.XPATH, "//*[@autocomplete='username']").send_keys("自己的账号")
        time.sleep(1)
        driver.find_element(By.XPATH, "//*[@autocomplete='current-password']").send_keys("密码")
        time.sleep(2)
        # 点击登陆
        driver.find_element(By.XPATH, "//*[@class='base-button']").click()
        time.sleep(10)
        # 关闭浏览器
    except Exception as e:
        print(e)
    finally:
        driver.close()

你可能感兴趣的:(python,python)