程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+seleniums custom browser engine class encapsulation

編輯:Python

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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved