Python+Selenium Custom browser engine class encapsulation for
Environmental Science :python3.8
Tools :PyCharm
launch_webdriver.py
from selenium import webdriver
from common import configs
from urllib3.exceptions import ProtocolError
import logging
class Browser(object):
"""
def __init__(self,browser_type):
self.browser_type = browser_type
def get_browser(self):
browser_type='chrome'
try:
if browser_type is None or browser_type=='chrome':
driver = webdriver.Chrome(executable_path=configs.chrome_path)
elif browser_type=='firefox':
driver = webdriver.Firefox()
elif browser_type == 'ie' :
driver = webdriver.Ie(executable_path=configs.ie_path)
else: driver = webdriver.Chrome(executable_path=configs.chrome_path)
driver.maximize_window()
driver.implicitly_wait(5)
return driver
except ProtocolError as e:
logging.warning('time=%s connect browser failed'%e)
except Exception as e:
logging.warning('unknown failure about browser!'%e)
if __name__ == "__main__":
driver = Browser('chrome').get_browser()
driver.get('http://www.baidu.com')
driver.maximize_window()
driver.implicitly_wait(6)
driver.quit()