python集成chrome浏览器实现程序自动化教程

前言

最近公司年底了来了个自动化项目,需要使用Python 集成 chromeDriver 实现加载chrome浏览器并实现自动化程序监控,结合使用socks协议实现某些特殊网路的连接。程序并不复杂,主要是在网络连接与多线程中出现的问题需要优化并合理平滑的过渡。

环境

python3.8
chrome 112,这里也可以使用其他版本浏览器
chromeDriver 建议与 chrome内核保持一致 ,这里使用112版本
所需要的依赖包:
selenium
requests
faker

实现代码

我这里使用pych作为代码编辑工具,仅供参考:

ebgl_vendors = ['Google Inc.', 'Microsoft', 'Apple Inc.', 'ARM', 'Intel Inc.', 'Qualcomm']

systems = ['Win32', 'Linux i686', 'Linux armv7l', 'MacIntel']

 def windowbounds(self, type:str='box', startX:int=0, startY:int=0, width:int=500, height:int=500, col:int=3, spaceX:int=0, spaceY:int=0, offsetX:int=0, offsetY:int=0):
        """
        窗口排列
        : params:type: *排列方式,宫格 box ,对角线 diagonal
        : params:startX: *Int起始X位置,默认0
        : params:startY: *Int起始Y位置,默认0
        : params:width: *Int宽度,最小500
        : params:height: *Int高度,最小200
        : params:col: *Int宫格排列时,每行列数
        : params:spaceX: *Int宫格横向间距,默认0
        : params:spaceYInt宫格纵向间距,默认0:
        : params:offsetX: *Int对角线横向偏移量
        : params:offsetY: *Int对角线纵向偏移量
        """
        if type not in ['box', 'diagonal']:
            logger.error('[!] 排列方式错误,自动选用box')
            type = 'box'
        if width < 500:
            logger.error('[!] 宽度最小500,自动设置为500')
            width = 500
        if height < 200:
            logger.error('[!] 高度最小200,自动设置为200')
            height = 200
        payload = {
            'type': type,
            'startX': startX,
            'startY': startY,
            'width': width,
            'height': height,
            'col': col,
            'spaceX': spaceX,
            'spaceY': spaceY,
            'offsetX': offsetX,
            'offsetY': offsetY
        }
        data = self.__request('windowbounds', payload)
        if data.get('success'):
            logger.info(f'[*] 窗口排列成功')
            return True
        else:
            logger.error(f'[!] 窗口排列失败')
            return False


 options = webdriver.ChromeOptions()
            ws_url = data['data']['http']
            options.add_experimental_option('debuggerAddress', ws_url)
            options.arguments.extend(["--no-default-browser-check", "--no-first-run"])
            options.arguments.extend(["--no-sandbox", "--test-type"])
            #options.arguments.extend(["excludeSwitches", ["enable-automation"]])
            #options.arguments.extend(["useAutomationExtension", False])
            # 指定chrome的路径
            # 谷歌浏览器位置
            chrome_location = r'C:\Program Files\Google\Chrome\Application\chrome.exe'
            options.binary_location = chrome_location
            chrome_path = r"C:\Users\gzz\AppData\Local\Programs\Python\Python37\chromedriver.exe"
            driver = webdriver.Chrome(options=options)
            # 随机位置
            x = random.randint(0, 500)
            y = random.randint(10, 200)
            driver.set_window_position(x, y)

其他 设置socks部分代码 大家自行设置,我这里已在本机设置好了socks5环境。
经过测试,在自动化的程序中,浏览器加载过程中由于无法请准控制url所以流量消耗较大,这里推荐大家一个比较优惠的socks流量网站:
【全球代理IP免费用】!免费用!免费用!
全球200+国家,9000w+动态IP,没任何费用!
动态住宅IP/静态住宅IP/机房IP等,永久免费!

官网:https://my.socks5.io#UYADLCUCHR
点击注册,永久免费海外IP.
使用我提供的链接可免费领取不限IP流量,同时购买也具有一定的优惠。

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

火焰蔷薇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值