scrapy版登录一个案例:selenium和scrapy之间的结合_范之度的博客-CSDN博客_scrapy和selenium结合
常见的模板:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
from utils.config import *
import pymysql
import re
import time
class Mailbox(object):
def __init__(self):
"""
初始化
"""
self.url = URL
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser, TIME_OUT) # 显式等待时间10秒
# 邮箱账号密码
self.mail_user = MAIL_USER
self.mail_pass = MAIL_PASS
# MySql用户名密码
# self.user = MYSQL_USER
# self.pass_ = MYSQL_PASS
def __del__(self):
self.browser.close()
def login_email(self):
"""
登陆网页版网易163邮箱
:return: None
"""
self.browser.get(self.url)
# 获取iframe节点
frame_1 = self.wait.until(EC.presence_of_element_located((By.XPATH,
'//*[@id="loginDiv"]/iframe')))
# 切换Frame
self.browser.switch_to.frame(frame_1)
time.sleep(3)
# 账号输入框
user = self.wait.until(EC.presence_of_element_located((By.NAME, 'email')))
# 密码输入框
password = self.wait.until(EC.presence_of_element_located((By.NAME, 'password')))
# 登录按钮
button = self.wait.until(EC.element_to_be_clickable((By.ID, 'dologin')))
# 输入账号
user.clear()
user.send_keys(self.mail_user)
time.sleep(3)
# 输入密码
password.clear()
password.send_keys(self.mail_pass)
time.sleep(3)
# 登录
button.click()
time.sleep(10)
# 获取登录成功后界面左上角的Logo,验证登录成功.
logo = self.wait.until(EC.presence_of_element_located((By.ID, 'h1Logo')))
if logo:
print('Successful login!')
主要语句:
self.browser = webdriver.Chrome()
self.wait = WebDriverWait(self.browser, TIME_OUT) # 显式等待时间10秒
self.browser.get(self.url)
# 获取iframe节点
frame_1 = self.wait.until(EC.presence_of_element_located((By.XPATH,'//*[@id="loginDiv"]/iframe')))
# 切换Frame
self.browser.switch_to.frame(frame_1)
time.sleep(3)
# 账号输入框
user = self.wait.until(EC.presence_of_element_located((By.NAME, 'email')))
# 密码输入框
password = self.wait.until(EC.presence_of_element_located((By.NAME, 'password')))
# 登录按钮
button = self.wait.until(EC.element_to_be_clickable((By.ID, 'dologin')))
# 输入账号
user.clear()
user.send_keys(self.mail_user)
time.sleep(3)
# 输入密码
password.clear()
password.send_keys(self.mail_pass)