from selenium import webdriver
class Browser():
def get_driver(self):
driver=webdriver.Chrome()
driver.maximize_window()
return driver
if __name__ == '__main__':
br=Browser()
br.get_driver()
base_page.py
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 BasePage
from time import sleep
class 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()