前言
大家好,我是yma16,本文分享selenuim登录csdn。
什么是selenuim
Selenium是一个开源的自动化测试框架,主要用于自动化测试Web应用程序。它支持多种编程语言,包括Java、Python、C#等。Selenium可以模拟用户在浏览器中的操作,比如点击、输入文本、下拉菜单等,然后检查应用程序的响应。它可以运行于不同的浏览器和操作系统上。Selenium被广泛应用于Web应用程序的测试和自动化集成测试。
环境准备
edgdriver下载与环境变量设置
与自己的edg版本一致
根据路径添加环境变量
selenuim驱动
csdn的登录界面分析
切换到账号密码登录
查找账号密码登录的标签点击即可,这里我使用链接文本查找
- find_element_by_link_text(‘账号密码登录’)
用户名的查找,这里我使用id查找,id=“all”
- find_element_by_id(‘all’)
密码的查找,这里我使用id查找,id=“password-number” - find_element_by_id(‘password-number’)
登录按钮的查找,使用xpath
- find_element_by_xpath(‘//button[@data-type=“account” and @class=“btn btn-primary”]’)
传递参数和点击
bijjiandan
- send_keys(参数)
- click()
运行即可登录
from selenium import webdriver import time dir_path = 'msedgedriver.exe' driver = webdriver.Edge(executable_path=dir_path) url = 'https://passport.csdn.net/login?code=public' driver.get(url) now_url = driver.current_url def login_csdn(): try: time.sleep(1) textlogin = driver.find_element_by_link_text('账号密码登录')#账号密码登录的链接a标签 textlogin.click()#点击 time.sleep(1) name=driver.find_element_by_id('all') pwd=driver.find_element_by_id('password-number') name.send_keys('name') time.sleep(1) pwd.send_keys('pwd') time.sleep(1) btn=driver.find_element_by_xpath('//button[@data-type="account" and @class="btn btn-primary"]') btn.click() except Exception as e: print(e) return False return True if __name__=='__main__': login_csdn()
运行结果正确!
结束
本文分享到这结束,如有错误或者不足之处欢迎指出,感谢大家的阅读!