zoukankan      html  css  js  c++  java
  • 使用selenium进行浏览器中途调试的方法类实现

    """
    备注:
    备注:
    浏览器断点调试,不需要每次都要重新跑 类实现
    """
    import os
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    import socket
    
    
    # 单独打开浏览器,使用9222端口,信息保存到E:/pythonwork/testfile
    # 请在这个浏览器下面安装xpath-help儿插件
    class DebugBrowser:
        def __init__(self):
            self.ip = '127.0.0.1'
            self.port = 5003
            self.user_file = 'E:/Py_selenium/auto/'
            self.chrome_option = Options()
    
        def debug_chrome(self):
            """
            :return: chrome_option 浏览器调试选项
            """
            # 判断是否已经启动调试端口,已启动直接添加监听选项
            if self.check_port():
                self.chrome_option.add_experimental_option('debuggerAddress', '{}:{}'.format(self.ip, self.port))
            # 未启动则重新启动浏览器并监听调试端口
            else:
                os.popen('cd C:/Users/antony/AppData/Local/Google/Chrome/Application/ & chrome.exe --remote-debugging-port="{}" --user-data-dir="{}"'.format(self.port, self.user_file))
                self.chrome_option.add_experimental_option('debuggerAddress', '{}:{}'.format(self.ip,  self.port))
            return self.chrome_option
    
        def check_port(self):
            """
            判断调试端口是否监听
            :return:check 是否监听
            """
            sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            result = sock.connect_ex((self.ip, self.port))
            if result == 0:
                check = True
            else:
                check = False
            sock.close()
            return check
      
    #  使用方法
    #  如果在该端口存在浏览器,直接使用该浏览器启动driver
    #  如果不存在,则在端口打开浏览器,然后再在该端口启动driver
    #if __name__ == '__main__':
    driver=webdriver.Chrome(options=DebugBrowser().debug_chrome())
    #driver.get('https://www.baidu.com')
    driver.get('https://www.qq.com')
  • 相关阅读:
    hibernate_0100_HelloWorld
    MYSQL子查询的五种形式
    JSF是什么?它与Struts是什么关系?
    nop指令的作用
    htmlparser实现从网页上抓取数据(收集)
    The Struts dispatcher cannot be found. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the
    FCKeditor 在JSP上的完全安装
    Java遍历文件夹的2种方法
    充电电池和充电时间说明
    吃知了有什么好处
  • 原文地址:https://www.cnblogs.com/51testing/p/13870503.html
Copyright © 2011-2022 走看看