from selenium import webdriverclass Browser(): def get_driver(self): driver=webdriver.Chrome() driver.maximize_window() return driverif __name__ == '__main__': br=Browser() br.get_driver()
from driver import *class BasePage(Browser): def __init__(self): self.driver=self.get_driver() def open(self,url): self.driver.get(url) def is_element_exist(self,element): flag=True try: self.driver.find_element_by_css_selector(element) return flag except: flag=False return flag
from base_page import BasePagefrom time import sleepclass Test_Baidu(BasePage): def check_url(self): self.open("https://www.baidu.com") sleep(1) element=self.is_element_exist('[name="tj_trnews"]') if element: print("存在") self.open("https://www.taobao.com") sleep(5) else: print("不存在") self.open("https://jd.com")if __name__ == '__main__': tb=Test_Baidu() tb.check_url()input()