1.sleep: 进程休眠
--- 等待固定的时间,如等待5秒:sleep(5)
2.implicitly_wait(30):等待页面完全加载完成 属于隐式等待
--页面加载完成的标志是左上角转圈结束
--如果页面元素加载完成了,某些js加载失败,页面左上角一直转圈,会耗费时间
-- 全局的,只写一次就行了
-- 缺点:页面有跳转的时候,它不知道去等跳转后的页面
3. WebDriverWait(self,driver, timeout, poll_frequency=POLL_FREQUENCY,ignored_exceptions=None)
显示等待
--driver:打开浏览器的一个实例参数,这个不用多说
--timeout:超时的总时长30s
--poll_frequency:循环去查询的间隙时间,默认0.5秒
--ignored_exceptions:忽略异常,默认忽略NoSuchElementException
---------------------------
显示等待示例:
上一节中记流水账的登录代码:
# coding=utf-8 from selenium import webdriver import time profile_path = r'C:\Users\songping\AppData\Roaming\Mozilla\Firefox\Profiles\gjihjypg.default' profile = webdriver.FirefoxProfile(profile_path) driver = webdriver.Firefox(profile) url = "http://192.168.0.101/phpwind/admin.php#initiator" driver.get(url) time.sleep(5) driver.find_element_by_name("admin_name").send_keys("admin") driver.find_element_by_name("admin_pwd").send_keys("123456") driver.find_element_by_name("submit").click() #登录成功之后 login_xpath = 'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]' result =driver.find_element_by_xpath(login_xpath).text print("获取到的登录结果:%s"%result) assert result == "[注销]"
加入显示等待,改进代码为: # coding=utf-8 from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait profile_path = r'C:\Users\songping\AppData\Roaming\Mozilla\Firefox\Profiles\gjihjypg.default' profile = webdriver.FirefoxProfile(profile_path) driver = webdriver.Firefox(profile) url = "http://192.168.0.102/phpwind/admin.php#initiator" driver.get(url) #driver.find_element_by_name("admin_name").send_keys("admin") ele1 = WebDriverWait(driver, 10).until(lambda x: x.find_element("name","admin_name")) ele1.send_keys("admin") #driver.find_element_by_name("admin_pwd").send_keys("123456") ele2 = WebDriverWait(driver, 10).until(lambda x: x.find_element("name","admin_pwd")) ele2.send_keys("123456") #driver.find_element_by_name("submit").click() ele3 = WebDriverWait(driver, 10).until(lambda x: x.find_element("name","submit")) ele3.click() #登录成功之后 login_xpath = 'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]' result =driver.find_element_by_xpath(login_xpath).text print("获取到的登录结果:%s"%result) assert result == "[注销]"
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait class Base(): def __init__(self): self.driver = driver def find(self,locator): ele = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(*locator)) return ele def send(self,locator,text): ele =self.find(locator) ele.send_keys(text) def click(self,locator): self.find(locator).click() def clear(self,locator): self.find(locator).clear() def get_text(self, locator): ele =self.find(locator) return ele.text if __name__ == '__main__': driver = webdriver.Firefox() driver.get("http://192.168.0.102/phpwind/admin.php#initiator") loc1 = ("name","admin_name") loc2 = ("name", "admin_pwd") loc3 = ("name", "submit") login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]') b = Base() #实例 b.send(loc1,"admin") b.send(loc2, "123456") b.click(loc3) t = b.get_text(login_xpath) print(t) assert t == "[注销]"
------------------------
封装好后写个登录模块,调用base模块:
1.base代码:
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait class Base(): def __init__(self,driver): self.driver = driver def find(self,locator): ele = WebDriverWait(self.driver, 10).until(lambda x: x.find_element(*locator)) return ele def send(self,locator,text): ele =self.find(locator) ele.send_keys(text) def click(self,locator): self.find(locator).click() def clear(self,locator): self.find(locator).clear() def get_text(self, locator): ele =self.find(locator) return ele.text if __name__ == '__main__': from selenium import webdriver driver = webdriver.Firefox() driver.get("http://192.168.0.102/phpwind/admin.php#initiator") loc1 = ("name","admin_name") loc2 = ("name", "admin_pwd") loc3 = ("name", "submit") login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]') b = Base() #实例 b.send(loc1,"admin") b.send(loc2, "123456") b.click(loc3) t = b.get_text(login_xpath) print(t) assert t == "[注销]"
2.login_fuc 登录模块,登录模块调用base模块:
from t7.base import Base class Login(Base): loc1 = ("name","admin_name") loc2 = ("name", "admin_pwd") loc3 = ("name", "submit") login_xpath = ("xpath",'html/body/table/tbody[1]/tr[1]/td/div/div/p/a[2]') def login(self, user="admin", psw="123456"): self.send(self.loc1,user) self.send(self.loc2,psw) self.click(self.loc3) if __name__ == '__main__': from selenium import webdriver driver = webdriver.Firefox() driver.get("http://192.168.0.102/phpwind/admin.php#initiator") loginpage = Login(driver) loginpage.login()
标签:11,封装,name,admin,self,driver,locator,base,find From: https://www.cnblogs.com/pingzi66-ww/p/17100281.html