脚本说明
自动登录到论坛并签到
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support.wait import WebDriverWait"""step 1:https://www.selenium.dev/ 下载对应浏览器驱动,放在浏览器根目录例如:C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exestep 2: 下载包pip install seleniumstep 3:引入pom.py
"""# 技类
class BasePage:def __init__(self,url,driver:webdriver):self.driver = driverself.url = urlif not driver:self.driver = webdriver.Edge()driver.get(url)def lazyload(self, by:By, value)->WebElement:"""显性加载:param by: 定位元素的方式:param value: 表达式:return:"""return WebDriverWait(self.driver, 10).until(lambda x:x.find_element(by, value))def down_base64_png(self,srcStr:str, file_name:str='code.png'):"""下载验证码:return:"""import base64img_data = srcStr.split(',')[1]img_data = base64.b64decode(img_data)with open(file_name, 'wb') as f:f.write(img_data)def quit(self):self.driver.quit()class Ruike1(BasePage):def __init__(self, url:str='https://www.ruike1.com/',driver:webdriver=webdriver.Edge()):super().__init__(url,driver)def login(self,u_name,u_pwd):el_name = self.lazyload(By.ID,'ls_username')print(el_name)el_pwd = self.lazyload(By.ID,'ls_password')el_name.send_keys(u_name)el_pwd.send_keys(u_pwd)lg_btn = self.lazyload(By.TAG_NAME,'button')lg_btn.click()print('登录成功')time.sleep(1)sign_in = self.lazyload(By.ID,'fx_checkin_b')sign_in.click()print('签到成功')time.sleep(1)self.quit()if __name__ == '__main__':rkPage = Ruike1()rkPage.login('xxx','3333')