相信看了工厂模式的简介,一定能懂这样的设计思路了吧

    我把这段代码比作什么工厂好呢,就叫它: Driver工厂
    已配置环境的driver、没有配置的(准备用 .exe)、使用selenium gird的工厂

    当然使用gird的driver工厂,你需要传入的是master-node-url
    1610704328(1).png

    1. import abc
    2. from abc import ABC
    3. from selenium import webdriver
    4. from conf.ErrorConfig import GETDRIVERERROR, BROWEREXPECTION
    5. from selenium.webdriver.chrome.webdriver import WebDriver
    6. from conf.PathConfig import DRIVER_DIR
    7. class Driver(ABC):
    8. driver = None
    9. @abc.abstractmethod
    10. def product_driver(self, *args, **kwargs):
    11. pass
    12. @classmethod
    13. def get_driver(cls, *args, **kwargs) -> WebDriver:
    14. if cls.driver is None:
    15. try:
    16. cls.driver: WebDriver = cls().product_driver(*args, **kwargs)
    17. cls.driver.maximize_window()
    18. except:
    19. raise GETDRIVERERROR
    20. return cls.driver
    21. else:
    22. return cls.driver
    23. class Local(Driver):
    24. """
    25. 访问本地环境变量
    26. """
    27. def product_driver(self, browertype='chrome', debug=False):
    28. if debug:
    29. options = webdriver.ChromeOptions()
    30. options.debugger_address = "127.0.0.1:9222"
    31. driver = webdriver.Chrome(options=options)
    32. else:
    33. if browertype == 'firefox':
    34. driver = webdriver.Firefox()
    35. elif browertype == 'chrome':
    36. driver = webdriver.Chrome()
    37. else:
    38. raise BROWEREXPECTION
    39. return driver
    40. class test(Driver):
    41. """
    42. 未配置chromedriver环境变量
    43. """
    44. def product_driver(self, *args, **kwargs):
    45. driver = webdriver.Chrome(executable_path=DRIVER_DIR + 'chromedriver.exe')
    46. return driver
    47. class Grid(Driver):
    48. def product_driver(self, selenium_grid_url, brower='chrome'):
    49. """
    50. selenium gird
    51. :param selenium_grid_url: 访问gird server-node地址
    52. :param brower:
    53. :return:
    54. """
    55. des = webdriver.DesiredCapabilities
    56. if brower == 'firefox':
    57. capablities = des.FIREFOX.copy()
    58. elif brower == 'chrome':
    59. capablities = des.CHROME.copy()
    60. else:
    61. raise
    62. driver = webdriver.Remote(command_executor=selenium_grid_url,
    63. desired_capabilities=capablities)
    64. return driver
    65. class DriverFactory:
    66. """
    67. DriverFacory FOR chrome Firefox..
    68. """
    69. def __init__(self):
    70. pass
    71. def __new__(cls, *args, **kwargs):
    72. if not hasattr(DriverFactory, "_instance"):
    73. DriverFactory._instance = object.__new__(cls)
    74. return DriverFactory._instance
    75. @staticmethod
    76. def chooseBrower(env: str = 'test'):
    77. """
    78. choose env to product driver
    79. :param env:
    80. :param
    81. :return:
    82. """
    83. map_ = {"local": Local,
    84. 'test': test,
    85. "grid": Grid}
    86. if map_.get(env, None) is None:
    87. raise BROWEREXPECTION(env)
    88. return map_[env.lower()]
    89. if __name__ == "__main__":
    90. dri = DriverFactory().chooseBrower("test").get_driver()
    91. dri.get('https://www.baidu.com')

    看到结束
    白嫖不能忍,请发一次文