10.8模块化脚本

    科技2025-05-16  11

    from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By import time import requests

    path = r’D:\sele\chromedriver.exe’ wd = webdriver.Chrome(executable_path=path) def goto_url(url): wd.set_page_load_timeout(5) wd.get(url) wd.implicitly_wait(5) def login(usr,pwd): ele_user = wd.find_element_by_id(‘userName’) ele_user.clear() ele_user.send_keys(usr) ele_pwd =wd.find_element_by_id(‘passWord’) ele_pwd.clear() ele_pwd.send_keys(pwd) ele_btn = wd.find_element_by_xpath(’/html/body/form/div[4]/div/button’) ele_btn.click()

    def check(word): if word in wd.page_source: print(‘测试成功’) else: print(‘测试失败’)

    def flogin_success(): goto_url(‘http://192.168.1.251:9012/login’) login(‘qwen’, ‘q123456’) check(‘实训项目’)

    def flogin_fail(): goto_url(‘http://192.168.1.251:9012/login’) login(‘qwen’, ‘123456’) check(‘密码’) if name ==“main”: try: flogin_success() flogin_fail() except Exception as e: raise e finally: wd.quit()

    Processed: 0.012, SQL: 8