Win 环境 Firefox
1.FireFox 48以上版本 : Selenium 3.X +FireFox驱动——geckodriver
2.Firefox 48 以下版本: Selenium2.X 内置驱动
geckodriver下载地址 下载后放置在Python
的根目录。
浏览器位数的版本和驱动版本要一致! 如果是32bit浏览器而Driver是64bit则会导致脚本运行失败!
1 driver = webdriver.Firefox()
Chrome
1 driver = webdriver.Chrome()
IE
Edge 新旧版本区别 安装Edge 75以上的新版本,基于 Chromium 的新版 Microsoft Edge 于 2020 年 1 月 15 日发布。 它与 Windows 和 macOS 的所有受支持版本兼容。 下载的新版浏览器将替换 Windows 10 电脑上的旧版 Microsoft Edge。 新版浏览器具备出色的速度和性能、适用于网站和扩展的一流兼容性以及内置的隐私和安全功能,可全面满足你的需求。
旧版 Microsoft Edge 是基于 HTML 的浏览器,与 Windows 10 一起于 2015 年 7 月发布。 它是 Windows 10 电脑上的默认浏览器。
驱动安装
1 2 3 4 options = EdgeOptions() options.use_chromium = True #启动Chrome内核 options.executable_path =r'C:\python37\msedgedriver.exe' #指定driver路径 driver = Edge(options=options)
Mac 环境 Chrome
首先查看自己Chrome
版本号,然后下载对应的chromedriver
下载完成后把它放在我们usr/local/bin
下.
使用命令vim ~/.bash_profile
配置环境变量
1 2 PATH=/bin:/usr/bin:/usr/local/bin:${PATH} export PATH
在终端输入命令chromedriver
查看是否安装成功。
1 2 3 4 5 mac@Sutune bin % chromedriver Starting ChromeDriver 103.0.5060.53 (a1711811edd74ff1cf2150f36ffa3b0dae40b17f-refs/branch-heads/5060@{#853}) on port 9515 Only local connections are allowed. Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe. ChromeDriver was started successfully.
整体封装 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 class BrowserEngine (object ): dir = os.path.dirname(os.path.abspath('.' )) chrome_driver_path = dir + '/tools/chromedriver.exe' ie_driver_path = dir + '/tools/IEDriverServer.exe' download_path=dir +'/download' def __init__ (self, driver ): self.driver = driver def open_browser (self, driver, urlname ): config = configparser.ConfigParser() file_path = os.path.dirname(os.path.abspath('.' )) + '/config/config.ini' config.read(file_path) browser = config.get("browserType" , "browserName" ) logging.info("You had select %s browser." % browser) url = config.get("testServer" , urlname) logging.info("The test server url is: %s" % url) if browser == "Firefox" : driver = webdriver.Firefox() logging.info("Starting firefox browser." ) elif browser == "Chrome" : options = webdriver.ChromeOptions() prefs = {} prefs["credentials_enable_service" ] = False prefs["profile.password_manager_enabled" ] = False prefs["profile.default_content_setting_values.notifications" ] = 2 prefs["download.default_directory" ]=self.download_path options.add_experimental_option('prefs' , prefs) driver = webdriver.Chrome(self.chrome_driver_path,chrome_options=options) logging.info("Starting Chrome browser." ) elif browser == "IE" : driver = webdriver.Ie() logging.info("Starting IE browser." ) elif browser == "Edge" : options = EdgeOptions() options.use_chromium = True options.executable_path =r'C:\python37\msedgedriver.exe' driver = Edge(options=options) driver.get(url) logging.info("Open url: %s" % url) driver.maximize_window() logging.info("Maximize the current window." ) driver.implicitly_wait(10 ) logging.info("Set implicitly wait 10 seconds." ) return driver def quit_browser (self ): logging.info("Now, Close and quit the browser." ) self.driver.quit()
参考资料