from selenium import webdriver


#公共的,对driver初始化的部分做抽离
class BasePage:
    def __init__(self):
        opt = webdriver.ChromeOptions()
        opt.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=opt)
        self.driver.implicitly_wait(10)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
from selenium import webdriver

from selenium_po.page.basepage import BasePage
from selenium_po.page.contact_page import ContactPage

class MainPage(BasePage):
    def goto_contact_page(self):

        self.driver.find_element_by_id("menu_contacts").click()

        return ContactPage()
import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from selenium_po.page.basepage import BasePage


class ContactPage(BasePage):

    def click_add_member(self):
        from selenium_po.page.add_member_page import AddMemberPage

        ele = (By.CSS_SELECTOR, ".ww_operationBar .js_add_member")
        WebDriverWait(self.driver, 10).until(expected_conditions.element_to_be_clickable(ele))
        while True:
            # ele = driver.find_element_by_css_selector(".ww_operationBar .js_add_member")
            # ele.click()  #不停点,直到添加成员页面出现
            self.driver.find_element(*ele).click()# 解数组
            element = self.driver.find_elements_by_id("username")#用elements找到username,说明进入到添加成员页面
            if len(element) > 0:
                break


        return AddMemberPage()
    def get_member(self):
        time.sleep(2)
        eles = self.driver.find_elements_by_css_selector('.member_colRight_memberTable_td:nth-child(2)')
        name_list = []
        for value in eles:
            # 获取元素属性title的值,存入list内
            print(value.get_attribute("title"))
            # if value.get_attribute("title") == "七ds月3":
            #     return True
            # return False
            name_list.append(value.get_attribute("title"))
        # 断言目标名字是否在列表内
        return name_list
from selenium import webdriver

from selenium_po.page.basepage import BasePage


class AddMemberPage(BasePage):
    def add_member(self):
        from selenium_po.page.contact_page import ContactPage

        self.driver.find_element_by_id("username").send_keys("七ds月8")
        self.driver.find_element_by_id("memberAdd_english_name").send_keys("uiui")
        self.driver.find_element_by_id('memberAdd_acctid').send_keys("tgg6")
        self.driver.find_element_by_id("memberAdd_phone").send_keys("15904005545")
        self.driver.find_element_by_id("memberAdd_mail").send_keys("22096809@qq.com")
        self.driver.find_element_by_css_selector(".js_btn_save").click()
        return ContactPage()
from selenium_po.page.main_page import MainPage


class TestLogin:
    def setup(self):
        self.main = MainPage()
    def teardown(self):
        pass
    def test_login(self):
        namelist = self.main.goto_contact_page().click_add_member().add_member().get_member()
        print(namelist)
        assert "七ds月8" in namelist
1.执行test_login()方法后,报错信息如下:

selenium练习3【抽离driver公共方法】_初始化

2.原因分析

每次初始化都调用了主页:主页去通讯录页面,通讯录页面此时继承并调用basepage方法,basepage方法又访问了企业微信主页

3.解决方法
优化后的basepage方法:
from selenium import webdriver
#公共的,对driver初始化的部分做抽离
from selenium.webdriver.remote.webdriver import WebDriver

class BasePage:
    def __init__(self,base_url: WebDriver = None):
        #避免driver重复初始化
        if base_url is None:
            opt = webdriver.ChromeOptions()
            opt.debugger_address = "127.0.0.1:9222"
            self.driver = webdriver.Chrome(options=opt)
            self.driver.implicitly_wait(10)
        else:
            self.driver = base_url
优化后的main_page方法
from selenium import webdriver

from selenium_po.page.basepage import BasePage
from selenium_po.page.contact_page import ContactPage

class MainPage(BasePage):
    base_url = "https://work.weixin.qq.com/wework_admin/frame"
    def goto_contact_page(self):
        self.driver.find_element_by_id("menu_contacts").click()
        return ContactPage(self.driver)
4.发现的问题2

第一次base_url为空的时候会走BasePage的初始化方法,第二次实例化的时候不需要base_url为空
第一次访问企业微信‘首页’后,第二次操作就不需要访问‘首页’了,直接在通讯录中操作
所以在basepage中访问首页的时候,第一次就不需要传driver base,第二次在实例化的时候给它一个参数
return ContactPage(self.driver)
return AddMemberPage(self.driver)
return ContactPage(self.driver)