selenium 學習筆記 二次封裝常用的方法

2022-05-29 10:48:10 字數 1262 閱讀 1893

# coding = utf-8

'''二次封裝元素方法

加入迴圈查詢方法,提高查詢元素的穩定性

'''from selenium import webdriver

from selenium.webdriver.support.wait import webdriverwait

class base():

def __init__(self, driver):

# self.driver = webdriver.firefox()

self.driver = driver

# 查詢元素的過程封裝了迴圈查詢

def find_element(self, locator):

element = webdriverwait(self.driver, 30).until(lambda x: x.find_element(*locator))

return element

def send_keys(self, locator, text):

self.find_element(locator).send_keys(text)

def click(self, locator):

self.find_element(locator).click()

if __name__ == "__main__":

driver = webdriver.firefox()

driver.get("")

mydriver = base(driver)

# 元組

# id = "id"  

# xpath = "xpath"  

# link_text = "link text"  

# partial_link_text = "partial link text"

# name = "name"  

# tag_name = "tag name"   

# class_name = "class name"  

# css_selector = "css selector"

inp_loc = ("id", "kw")

# mydriver.find_element(inp_loc)

mydriver.send_keys(inp_loc, "bai")

but_loc = ("id", "su")

mydriver.click(but_loc)

優勢:可以使**更穩定、封裝了迴圈查詢的方法

selenium的二次封裝

本文來介紹,如何把常用的幾個webdriver的方法封裝到自己寫的乙個類中去,這個封裝過程叫二次封裝selenium方法。我們把開啟站點,瀏覽器前進和後退,關閉和退出瀏覽器這這個方法封裝到乙個新寫的類中去。我們按照如下層次結構在pycharm中新建兩個包和兩個.py檔案 上圖,baidu searc...

Selenium二次封裝(一)

from selenium.webdriver.support import expected conditions as ec from selenium import webdriver import time class seleniumdriver def init self,browser...

Android 介面卡 Adapter 二次封裝

介面卡父類 版權 建立者 shuquan 模組 描述 介面卡父類 public abstract class baseadapterextends recyclerview.adapter override public baseviewholder oncreateviewholder viewg...