使用selenium实现web自动化(三)-----输入用户名密码登录
创始人
2025-01-07 08:37:52
0

1、实现自动化登录某个页面,需要输入用户名和密码认证,导入模块和库

from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.chrome.options import Options import re import subprocess import time

2、设置Chrome浏览器跳过安全检查

chrome_options = Options() chrome_options.add_argument('--ignore-certificate-errors')  chrome_options.add_argument('--ignore-ssl-errors')

3、设置Chrome浏览器禁止证书检查

capabilities = DesiredCapabilities.CHROME.copy() capabilities['acceptInsecureCerts'] = True

4、使用Chrome WebDriver实例化WebDriver对象,并传入Chrome选项和DesiredCapabilities,启动Chrome浏览器并打开登录页面

driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) # 启动Chrome浏览器并打开登录页面 driver.get("https://www.baidu.com/")   #输入需要登录的地址

5、找到用户名和密码输入框并输入用户名和密码,其中"user_name"和"password"分别为登录页面用户名和密码输入框对应的id或name,该值通过开发者页面获取(按F12进入)。"user1"和"123456"分别为自定义的用户名和密码。

username_input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "user_name"))) username_input.send_keys("user1") time.sleep(1) password_input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "password"))) password_input.send_keys("123456") time.sleep(1)

6、找到登录按钮并点击

login_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "loginbtn"))) login_button.click() time.sleep(3)

 7、好了,附上完整源代码

from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.common.desired_capabilities import DesiredCapabilities from selenium.webdriver.chrome.options import Options import re import subprocess import time   # 设置Chrome浏览器跳过安全检查 chrome_options = Options() chrome_options.add_argument('--ignore-certificate-errors')  chrome_options.add_argument('--ignore-ssl-errors') # 设置Chrome浏览器禁止证书检查 capabilities = DesiredCapabilities.CHROME.copy() capabilities['acceptInsecureCerts'] = True driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities) # 启动Chrome浏览器并打开登录页面 driver.get("https://www.baidu.com/") time.sleep(3) # 找到用户名和密码输入框并输入用户名和密码 username_input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "user_name"))) username_input.send_keys("user1") time.sleep(1) password_input = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "password"))) password_input.send_keys("123456") time.sleep(1) # 找到登录按钮并点击 login_button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "loginbtn"))) login_button.click() time.sleep(3)

相关内容

热门资讯

一分钟秒懂!(德扑给好牌)外挂... 一分钟秒懂!(德扑给好牌)外挂辅助透视下载!(EV扑克)2025新版教程(2022已更新)(哔哩哔哩...
6分钟辅助挂!微扑克ai技术(... 6分钟辅助挂!微扑克ai技术(透明挂黑科技)外挂透明挂辅助安装(2020已更新)(哔哩哔哩);大家肯...
四分钟黑科技!德州hhpoke... 四分钟黑科技!德州hhpoker是真的吗,wepoker私局代打(透视)wpk教程(有挂解惑)1、在...
重大通报!wepoke有挂,a... 重大通报!wepoke有挂,aapoker插件,新2025版(有挂实锤)-哔哩哔哩;1、这是跨平台的...
2分钟精通!wpk有辅助挂吗(... 2分钟精通!wpk有辅助挂吗(透明挂黑科技)外挂透明挂辅助透视(2021已更新)(哔哩哔哩);wpk...
透视有挂!(aapoker辅助... 透视有挂!(aapoker辅助工具)外挂辅助黑科技!(德扑之星代打)分享教程(2025已更新)(哔哩...
玩家必看分享!wepoke透技... 玩家必看分享!wepoke透技巧,德州之星模拟器,可靠教程(有挂透明挂)-哔哩哔哩;1、点击下载安装...
第8分钟俱乐部!we-poke... 第8分钟俱乐部!we-poker正规吗,wpk辅助哪里买(透视)2025教程(有挂猫腻)1、该软件可...
七分钟了解!wpk微扑克外挂事... 您好,wpk微扑克外挂事件这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
明白辅助挂!(wepower)... 明白辅助挂!(wepower)外挂辅助透视下载!(Wepoke存在)必胜教程(2020已更新)(哔哩...