chromedriver.exe弹出窗口如何隐藏的问题

1,820 阅读2分钟

在实现selenium自动登录时发现的问题,chromedriver.exe总是会弹出窗口干扰我,所以想要解决一下。解决来源(在最后一条给出了有用的解决方案)

1.selenium设置有密码代理

  • 其思路是通过定制一个浏览器的插件来实现,上述功能在Chrome浏览器上可以完美使用。
  • 不能与无头模式一起使用chrome_options.add_argument('--headless')

2.selenium弹窗隐藏

源码

中间大概就处理了下验证码识别,然后自动登录

import os
import re
import time
import json
import requests
import MySQLdb
import pyautogui as pg
from lxml import etree
from selenium import webdriver

from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

# 密码代理部分
def create_proxyauth_extension(proxy_host, proxy_port,
                               proxy_username, proxy_password,
                               scheme='http', plugin_path=None):
    """Proxy Auth Extension

    args:
        proxy_host (str): domain or ip address, ie proxy.domain.com
        proxy_port (int): port
        proxy_username (str): auth username
        proxy_password (str): auth password
    kwargs:
        scheme (str): proxy scheme, default http
        plugin_path (str): absolute path of the extension

    return str -> plugin_path
    """
    import string
    import zipfile

    if plugin_path is None:
        plugin_path = 'chrome_proxyauth_plugin.zip'

    manifest_json = """
    {
        "version": "1.0.0",
        "manifest_version": 2,
        "name": "Chrome Proxy",
        "permissions": [
            "proxy",
            "tabs",
            "unlimitedStorage",
            "storage",
            "<all_urls>",
            "webRequest",
            "webRequestBlocking"
        ],
        "background": {
            "scripts": ["background.js"]
        },
        "minimum_chrome_version":"22.0.0"
    }
    """

    background_js = string.Template(
    """
    var config = {
            mode: "fixed_servers",
            rules: {
              singleProxy: {
                scheme: "${scheme}",
                host: "${host}",
                port: parseInt(${port})
              },
              bypassList: ["foobar.com"]
            }
          };

    chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});

    function callbackFn(details) {
        return {
            authCredentials: {
                username: "${username}",
                password: "${password}"
            }
        };
    }

    chrome.webRequest.onAuthRequired.addListener(
                callbackFn,
                {urls: ["<all_urls>"]},
                ['blocking']
    );
    """
    ).substitute(
        host=proxy_host,
        port=proxy_port,
        username=proxy_username,
        password=proxy_password,
        scheme=scheme,
    )
    with zipfile.ZipFile(plugin_path, 'w') as zp:
        zp.writestr("manifest.json", manifest_json)
        zp.writestr("background.js", background_js)

    return plugin_path


# 运行一次生成zip文件后,即可注释掉
proxyauth_plugin_path = create_proxyauth_extension(
    proxy_host="ip",
    proxy_port="port",
    proxy_username="xxxxx",
    proxy_password="xxxxx"
)


def login():
    import pytesseract
    from PIL import Image, ImageEnhance
    pytesseract.pytesseract.tesseract_cmd = 'TesseractOCR\\tesseract.exe'
    route, driver = get_captch()
    img = Image.open(route)

    # 灰度处理,把图片转换为灰度图片
    img = img.convert('L')

    # 二值化处理(二值化调整系数可以有意想不到的结果)
    threshold = 85
    table = []
    for i in range(256):
        if i < threshold:
            table.append(0)
        else:
            table.append(1)
    out = img.point(table, '1')
    img = out.convert('RGB')
    enhancer = ImageEnhance.Color(img)
    enhancer = enhancer.enhance(0)
    enhancer = ImageEnhance.Brightness(enhancer)
    enhancer = enhancer.enhance(2)
    enhancer = ImageEnhance.Contrast(enhancer)
    enhancer = enhancer.enhance(8)
    enhancer = ImageEnhance.Sharpness(enhancer)
    img = enhancer.enhance(20)
    code = pytesseract.image_to_string(img)
    code = re.findall("\w+", code)
    if not code:
        driver.close()
        login()
    url1 = 'xxxxxx'
    driver.get(url)
    driver.implicitly_wait(1)

    # 用户名密码
    username = 'xxxxxx'
    password = 'xxxxxx'
    
    elem_user = driver.find_element_by_name("username")
    elem_user.send_keys(username)
    elem_pwd = driver.find_element_by_name("password")
    elem_pwd.send_keys(password)
    elem_code = driver.find_element_by_name("captcha")
    elem_code.send_keys(code[0])
    elem_code.send_keys(Keys.ENTER)
    driver.get("url1")
    try:
        elem_summit = driver.find_element_by_xpath('//*[@id="continue"]/div[2]/button')
        elem_summit.click()
    except:
        driver.close()
        login()
        
    driver.maximize_window()
    driver.get(url2)
    driver.service.stop()

def get_captch():
    from bs4 import BeautifulSoup
    import time
    import urllib.request
    chrome_options = Options()
    prefs = {
        'profile.default_content_settings.popups': 0,
        'download.default_directory': 'E:\src',
        'profile.default_content_setting_values': {
            'images': 2,
            'notifications' : 2,
        },

    }
    chrome_options.add_experimental_option('prefs', prefs)
    chrome_options.add_extension(proxyauth_plugin_path)
    chrome_options.add_argument(
        'user-agent="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
        'Chrome/67.0.3396.99 Safari/537.36"'
    )

    chrome_options.add_experimental_option("detach", True)
    driver = webdriver.Chrome(options=chrome_options)
    url = 'xxxxxx'
    driver.get(url)
    js = 'var q=document.documentElement.scrollTop=10000'
    driver.execute_script(js)
    html = driver.page_source
    bf_one = BeautifulSoup(html, 'lxml')
    proxy_support = urllib.request.ProxyHandler({
        "http": "xxxxx:xxxxxx@ip:port",
        "https": "xxxxx:xxxxxx@ip:port"
    })
    opener = urllib.request.build_opener(proxy_support)
    opener.addheaders = [('user_agent',
                          'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 '
                          '(KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36')]
    urllib.request.install_opener(opener)
    if not os.path.exists('images/'):
      os.makedirs('images/')
    route = 'images/' + str(time.time()) + '.png'
    img_url2 = 'http://ip/' + bf_one.img.get('src')
    driver.get(img_url2)
    driver.save_screenshot(route)
    return route, driver

login()

程序总会有chromedriver.exe弹窗解决

进入webdriver.Chrome(options=chrome_options)的Chrome修改源码,用create_no_window解决。

from subprocess import PIPE, STDOUT, CREATE_NO_WINDOW
    def start(self):
        """
        Starts the Service.

        :Exceptions:
         - WebDriverException : Raised either when it can't start the service
           or when it can't connect to the service
        """
        try:
            cmd = [self.path]
            cmd.extend(self.command_line_args())
            self.process = subprocess.Popen(cmd, env=self.env,
            close_fds=platform.system() != 'Windows',
            stdout=self.log_file,
            stderr=self.log_file,
            stdin=PIPE,
            #这里是修改的地方
            creationflags=CREATE_NO_WINDOW)
       except:
       		.......