Python Webdriver 重新使用已经打开的浏览器实例

参考于:https://blog.csdn.net/qiqiyingse/article/details/81668517

第一次打开浏览器后不关闭,第二次不用重新打开浏览器直接在第一次打开的浏览器上进行访问,节省打开浏览器的时间。

下面以某验证码为例,只需要第一次访问打开浏览器,后面只需要在第一次打开的浏览器上访问,原本在第一次之后得访问截图对元素的定位报错,经改良代码如下。

目录:

captcha.py文件

    from selenium import webdriver
    from PIL import Image
    import time
    import base64
    
    from selenium.webdriver import Remote
    from selenium.webdriver.chrome import options
    from selenium.common.exceptions import InvalidArgumentException
    
    class ReuseFirefox(Remote):
    
        def __init__(self, command_executor, session_id):
            self.r_session_id = session_id
            Remote.__init__(self, command_executor=command_executor, desired_capabilities={})
    
        def start_session(self, capabilities, browser_profile=None):
            """
            重写start_session方法
            """
            if not isinstance(capabilities, dict):
                raise InvalidArgumentException("Capabilities must be a dictionary")
            if browser_profile:
                if "moz:firefoxOptions" in capabilities:
                    capabilities["moz:firefoxOptions"]["profile"] = browser_profile.encoded
                else:
                    capabilities.update({'firefox_profile': browser_profile.encoded})
    
            self.capabilities = options.Options().to_capabilities()
            self.session_id = self.r_session_id
            self.w3c = False
    
    
    def cpc(captchaID,num,random):
        url = 'http://zxgk.court.gov.cn/zhzxgk/captcha.do?captchaId='+ str(captchaID) +'&random='+str(random)+''
    
        if num == 1:
            browser = webdriver.Firefox(executable_path='./geckodriver.exe')
            global session_id
            global executor_url
            session_id = browser.session_id
            executor_url = browser.command_executor._url
            browser.get(url)
    
            time.sleep(1)
    
            browser.save_screenshot('./images/big.png')
            img = browser.find_element_by_xpath(r'/html/body/img')
            global left,top,right,bottom
            left = img.location['x']  # 验证码图片左上角横坐标
            top = img.location['y']  # 验证码图片左上角纵坐标
            right = left + img.size['width']  # 验证码图片右下角横坐标
            bottom = top + img.size['height']  # 验证码图片右下角纵坐标
            # browser.close()
            im = Image.open('./images/big.png')
            im_crop = im.crop((left, top, right, bottom))  # 这个im_crop就是从整个页面截图中再截出来的验证码的图片
            # print(im_crop)
            im_crop.save('./images/captcha.png')
            # os.remove('./images/big.png')
    
            with open("./images/captcha.png", "rb") as fs:
                base64_data = base64.b64encode(fs.read())
                s = base64_data.decode()
                cpc_base64 = 'data:image/jpeg;base64,%s' % s
                print(cpc_base64)
                # return cpc_base64
                return s
    
        if num != 1:
            browser = ReuseFirefox(command_executor=executor_url, session_id=session_id)
            print(browser.current_url)
            browser.get(url)
            # browser.refresh()
            time.sleep(1)
            res = browser.page_source
            print(res)
    
            browser.save_screenshot('./images/big.png')
            im = Image.open('./images/big.png')
            im_crop = im.crop((left, top, right, bottom))  # 这个im_crop就是从整个页面截图中再截出来的验证码的图片
            im_crop.save('./images/captcha.png')
            # os.remove('./images/big.png')
    
            with open("./images/captcha.png", "rb") as fs:
                base64_data = base64.b64encode(fs.read())
                s = base64_data.decode()
                cpc_base64 = 'data:image/jpeg;base64,%s' % s
                print(cpc_base64)
                # return cpc_base64
                return s
    
    if __name__ == '__main__':
        cpc('Firefox','4c6f6bfaf2594145982df01322798afd')

captcha_api.py文件

from flask import Flask, request, jsonify

from captcha import cpc

app = Flask(__name__)
num = 0
app.config['JSON_AS_ASCII'] = False
@app.route('/captcha')

def hello_world():
    captchaID = request.values['captchaID']
    random = request.values['random']
    # num = request.values['num']
    print(captchaID)
    global num
    num += 1
    data = cpc(captchaID,num,random)
    print("==>",data, type(data))
    return jsonify(data)

if __name__ == '__main__':
    app.run(port=8083, debug=True,host='0.0.0.0')
    # app.run(port=8081, debug=True,host='10.10.23.43')

你可能感兴趣的:(Python Webdriver 重新使用已经打开的浏览器实例)