python selenium 实现自动登录豆瓣网
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/