python selenium 实现自动登录豆瓣网

作者: tww844475003 分类: Python 发布时间: 2021-05-22 16:35
from selenium import webdriver
import time

# 打开浏览器
browser = webdriver.Chrome()
browser.get('https://accounts.douban.com/passport/login?source=main')

# 点击密码登录
browser.find_element_by_class_name('account-tab-account').click()

# 输入帐号密码
browser.find_element_by_id('username').send_keys('******')  # 登录帐号
browser.find_element_by_id('password').send_keys('******')  # 登录密码

# 点击登录
browser.find_element_by_link_text('登录豆瓣').click()

time.sleep(5) # 等待
browser.quit() # 退出

如果运报错:

Traceback (most recent call last):
  File "D:\python\study\venv\lib\site-packages\selenium\webdriver\chrome\service.py", line 66, in start
    self.service_args, env=env, stdout=PIPE, stderr=PIPE)
  File "D:\python\study\venv\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "D:\python\study\venv\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:/python/study/requests/login/login.py", line 16, in <module>
    driver = webdriver.Chrome(executable_path=chromedriver,chrome_options=chrome_options)#对应的chromedriver的放置目录
  File "D:\python\study\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 61, in __init__
    self.service.start()
  File "D:\python\study\venv\lib\site-packages\selenium\webdriver\chrome\service.py", line 73, in start
    os.path.basename(self.path), docs_msg)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

找不到 chromedriver.exe

解决办法
去淘宝库下载对就chrome驱动,放入 D:\python\study\venv\Scripts
http://npm.taobao.org/mirrors/chromedriver/

前端开发那点事
微信公众号搜索“前端开发那点事”

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注