Loading

Selenium 启动Webdriver问题汇总

webdriver-manage用法

https://www.cnblogs.com/zibinchen/p/14310637.html

如果研习该篇文档,以下webdriver各类问题即可忽略,不必再看

安装selenium版本

命令行输入pip install selenium

pip install selenium

准备各浏览器webdriver

建议:不要使用当前时间最新的浏览器版本,因为新版不一定稳定,容易产生一系列问题,往前推一两个版本为合适

禁止chrome升级办法

mac:

# 在终端输入:
cd ~/Library/Google
sudo chown root:wheel GoogleSoftwareUpdate
# 打开chrome-帮助-关于chrome,看效果

Chrome浏览器

历史版本https://dl.lancdn.com/landian/soft/chrome/m/?utm_sources=DownPageBot

ChromeDriver仓库http://chromedriver.storage.googleapis.com/index.html

Firefox浏览器

【GeckoDriver仓库】https://github.com/mozilla/geckodriver/releases
GeckoDriver用于驱动Firefox,在这之前请确保已经正确安装好了Firefox浏览器并可以正常运行。

Edge浏览器

Microsoft Edge Webdriverhttps://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

Microsoft Edge Brower

链接:https://pan.baidu.com/s/1fmCLge1l80i8ERHNuTjeQg

提取码:frjy

【关闭Edge自动更新】

  1. WIN+R,输入services.msc
  2. 找到Windows Upadate双击,选择禁用,点击确定

webdriver安放位置

可以随便放,但需要知道路径

建议放在启动配置文件同级目录下

chrome指定下载路径并允许下载多个文件

elif browser == "chrome":
        from selenium.webdriver.chrome.options import Options
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': excelpath, "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs) # 指定下载路径,并且允许下载多个文件
        driver = webdriver.Chrome(
            executable_path=chrome_driver,
            chrome_options=options)
        return driver

Selenium启动webdriver写法

封装好启动webdriver的方法

注:通过webdriver-manager自动下载driver

#!/usr/config/env python
# -*- coding:utf-8 -*-

from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.microsoft import EdgeChromiumDriverManager
from webdriver_manager.microsoft import IEDriverManager
from webdriver_manager.opera import OperaDriverManager
from config.conf import cm


def browser(browser='chrome'):
    if browser == "firefox" or browser == "ff":
        profile = webdriver.FirefoxProfile()
        profile.set_preference('browser.download.dir', cm.download_path)  # 相同类型的文件上一次的下载保存位置
        profile.set_preference('browser.download.folderList',
                               2)  # 设置Firefox的默认下载文件夹。0是桌面;1是“我的下载”;2是自定义。这一选项你可以直接在“首选项-下载-将所有文件保存在此文件夹”中修改。但请注意,如果你在“首选项-下载-保存每个文件夹前询问存储位置“(也就是要求弹出下载对话框),那么本选项无效
        profile.set_preference('browser.download.manager.showWhenStarting',
                               False)  # 'application/octet-stream'对应下载的文件格式是rar
        profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_profile=profile)
    elif browser == "ff_headless":
        profile = webdriver.FirefoxProfile()
        profile.set_preference('browser.download.dir', cm.download_path)
        profile.set_preference('browser.download.folderList', 2)
        profile.set_preference('browser.download.manager.showWhenStarting', False)
        profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'application/octet-stream')
        from selenium.webdriver.firefox.options import Options
        firefox_options = Options()
        firefox_options.add_argument('--headless')
        driver = webdriver.Firefox(executable_path=GeckoDriverManager().install(), firefox_profile=profile,
                                   options=firefox_options)
    elif browser == "chrome":
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': cm.download_path,
            "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs)  # 指定下载路径,并且允许下载多个文件
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
    elif browser == "chrome_headless":
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': cm.download_path,
            "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs)
        options.add_argument('-headless')
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
    elif browser == "chrome_linux":
        options = webdriver.ChromeOptions()
        prefs = {
            'profile.default_content_settings.popups': 0,
            'download.default_directory': cm.download_path,
            "profile.default_content_setting_values.automatic_downloads": 1}
        options.add_experimental_option('prefs', prefs)
        options.add_argument('-headless')
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-dev-shm-usage')
        driver = webdriver.Chrome(executable_path=ChromeDriverManager().install(), options=options)
    elif browser == "edge":
        driver = webdriver.Edge(executable_path=EdgeChromiumDriverManager().install())
    elif browser == "IE":
        driver = webdriver.Ie(executable_path=IEDriverManager().install())
    elif browser == "opera":
        driver = webdriver.Opera(executable_path=OperaDriverManager().install())
    return driver


if __name__ == '__main__':
    browser('chrome')
``` 
posted @ 2020-01-15 14:28  Binzichen  阅读(1338)  评论(0编辑  收藏  举报