• web测试-赠送课程


    参考链接 

    官网:https://www.selenium.dev/

    (下载IDE)

    https://www.selenium.dev/selenium/docs/api/py/

    chromedriver下载路径:https://npm.taobao.org/mirrors/chromedriver/

    selenium IDE的安装

    firefox的安装

     学会找报错信息,以及理解报错信息的含义

    1.从后往前看,找到最后几个E

    2.找到【当前文件名:行号】,点到代码中,分析是否由于书写问题,定位的问题

    隐式等待

    隐式等待:动态地等待原色,最好在实例化driver之后立刻去设置隐式的等待

     1.先配置环境变量

    右键-属性

     配置到path环境变量里

     

     cmd里输入

    chrome --remote-debugging-port=9222

    需要重启chrome浏览器才能生效

     通过http://127.0.0.1:9222/

    可以验证是否配置成功

    # Generated by Selenium IDE
    import pytest
    import time
    import json
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    
    class TestTestdemo0():
        def setup_method(self, method):
            option = Options()
            option.debugger_address = '127.0.0.1:9222'
            self.driver = webdriver.Chrome(options = option)
            self.vars = {}
    
        def teardown_method(self, method):
            self.driver.quit()
    
        def test_testdemo0(self):
            # self.driver.get("https://ceshiren.com/")
            self.driver.set_window_size(1382, 754)
            self.driver.find_element(By.LINK_TEXT, "所有分类").click()
            class_ddd = self.driver.find_element(By.LINK_TEXT, "所有分类")
            print(class_ddd.get_attribute("class"))
            assert  'active' == class_ddd.get_attribute("class")

    # Generated by Selenium IDE
    import shelve
    
    import pytest
    import time
    import json
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    
    class TestTestdemo0():
        def setup_method(self, method):
            self.driver = webdriver.Chrome()
            # option = Options()
            # option.debugger_address = '127.0.0.1:9222'
            # self.driver = webdriver.Chrome(options = option)
            self.vars = {}
    
        def teardown_method(self, method):
            self.driver.quit()
    
        def test_cookie(self):
            self.driver.get("https://work.weixin.qq.com/wework_admin/frame#index")
            # cookies = self.driver.get_cookies()
            # print(cookies)
            cookies =[{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '2576825472129297'}, {'domain': 'work.weixin.qq.com', 'expiry': 1614025265, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '212r40h'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'expiry': 1645529729, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'expiry': 1616585732, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh'}]
            for cookie in cookies:
                self.driver.add_cookie(cookie)
    
            self.driver.get("https://work.weixin.qq.com/wework_admin/frame#index")
            time.sleep(3)
    
        def test_shelve(self):
            cookies = [
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False,
                 'value': '2576825472129297'},
                {'domain': 'work.weixin.qq.com', 'expiry': 1614025265, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/',
                 'secure': False, 'value': '212r40h'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False,
                 'value': 'direct'},
                {'domain': '.work.weixin.qq.com', 'expiry': 1645529729, 'httpOnly': False, 'name': 'wwrtx.c_gdpr',
                 'path': '/', 'secure': False, 'value': '0'},
                {'domain': '.work.weixin.qq.com', 'expiry': 1616585732, 'httpOnly': False, 'name': 'wwrtx.i18n_lan',
                 'path': '/', 'secure': False, 'value': 'zh'}]
    
            db = shelve.open('./mydbs/cookies')
            db['cookie'] = cookies
            db.close()

     如何获取cookies

    先复用浏览器,然后打印cookies

    复用浏览器步骤:

    1,cmd中输入:chrome --remote-debugging-port=9222

    2,在弹出的浏览器中打开企业微信,然后扫码登录企业微信

    3,代码如下:

    # Author:Han
    # python:3.9
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    class TestGetCookies:
        def setup(self):
            option = Options()
            option.debugger_address='127.0.0.1:9889'
            self.driver = webdriver.Chrome(options=option)
            # self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)  # 隐式等待
    
        def teardown(self):
            pass
    
        def test_getcookie(self):
            cookies = self.driver.get_cookies()
            print(cookies)
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    
    class TestTestdemo0():
        def setup_method(self, method):
            option = Options()
            option.debugger_address = '127.0.0.1:9222'
            self.driver = webdriver.Chrome(options=option)
    
        def teardown_method(self, method):
            self.driver.quit()
    
        def test_cookie(self):
            cookies = self.driver.get_cookies()
            print(cookies)

     4,将打印的cookies复制,就获取了cookies

    获取到cookies之后,就不用复用浏览器了,直接用cookies传入登录信息

    import time
    
    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    
    
    class TestDemo:
        def setup(self):
            # option = Options()
            # option.debugger_address = '127.0.0.1:9222'
            # self.driver = webdriver.Chrome(options=option)
            self.driver =webdriver.Chrome()
            self.driver.implicitly_wait(5)
    
        def teardown(self):
            pass
    
        def test_demo(self):
            # cookies = self.driver.get_cookies()
            # print(cookies)
            self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
            cookies = [{'domain': '.qq.com', 'expiry': 1614607133, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688851249090396'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688851249090396'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '9748397056'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '1494316890'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325033411576'}, {'domain': '.qq.com', 'expiry': 1614687677, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.863358477.1614601266'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'HwY4ZsCGmxPh6xSr_anuufNSquUoxye2Gy-ndCOgKmbcKtzTmQ72JiPSAoTR8-yv'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a1821517'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1614601266'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '2576825471264635'}, {'domain': 'work.weixin.qq.com', 'expiry': 1614632800, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '2nu1rhr'}, {'domain': '.qq.com', 'expiry': 1616550822, 'httpOnly': False, 'name': 'ptui_loginuin', 'path': '/', 'secure': False, 'value': '1219964985'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/', 'secure': False, 'value': '584415353'}, {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': '4fd65c76325acf822b8b528a15f2a392297a3039452f348bfeb11c12a0fc3b62'}, {'domain': '.work.weixin.qq.com', 'expiry': 1617199077, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh'}, {'domain': '.qq.com', 'expiry': 1677673277, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1688755123.1613182480'}, {'domain': '.qq.com', 'expiry': 1897288226, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': '9ab0ba39789428ab'}, {'domain': '.work.weixin.qq.com', 'expiry': 1641100817, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': '8rtbdgOvW4-Pwq4GD-d1FyrzjyvHObw6VQKzwIGJmzMchan5_EMrQb13KS0tjUFwSkwi9KfOp6khp4i2y64UQKErTsIM82uZluBBKRG3XJy9hdO32Cc7NDL25DbA-UkJn3AdxXhX4KtXiu620RaQVACELRDoQOOgeFdhgmUxeuFz-_2m9EAPpFY0ggIt-SDkqIITRmhtdZFZlcqFjtPOXKVrDRlrAB1YLvOj92BVSWDdI7uFtNCnlWeqVlxW7J4wco47GfW-RvsgzoJZ4kmC7w'}, {'domain': '.work.weixin.qq.com', 'expiry': 1646137265, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1613993294,1614340017,1614434715,1614601266'}, {'domain': '.qq.com', 'expiry': 1904971625, 'httpOnly': False, 'name': 'pac_uid', 'path': '/', 'secure': False, 'value': '1_584415353'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'n8KwV1gpaD'}]
            for cookie in cookies:
                self.driver.add_cookie(cookie)
            self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
            time.sleep(5)

     注意事项:

    获取的cookies中有个expiry,如果expiry为浮点型,则执行上述代码,会报错,

    因为cookie不支持浮点型

    解决办法就是删掉获取的cookies中的expiry

     for cookie in cookies:
                if 'expiry' in cookie.keys():
                    cookie.pop('expiry')
                self.driver.add_cookie(cookie)

     cookies如果数据太多,就可以使用shelve放到一个小型数据库里面

        def test_shelve(self):
            cookies = [
                {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False,
                 'value': '1688851249090396'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False,
                 'value': '1688851249090396'},
                {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/',
                 'secure': False, 'value': '9748397056'},
                {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/',
                 'secure': False, 'value': '1494316890'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False,
                 'value': '1970325033411576'},
                {'domain': '.qq.com', 'expiry': 1614767746, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False,
                 'value': 'GA1.2.863358477.1614601266'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False,
                 'value': 'HwY4ZsCGmxPh6xSr_anuuSbfXT47jQDTAoJO2AXYb9Z4Z0C5saVlGJ_Tixb1wGn-'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False,
                 'value': 'a9134412'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d',
                 'path': '/', 'secure': False, 'value': '1614681337'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False,
                 'value': '34522847332136937'},
                {'domain': 'work.weixin.qq.com', 'expiry': 1614712006, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/',
                 'secure': False, 'value': '2da5fhc'},
                {'domain': '.qq.com', 'expiry': 1616550822, 'httpOnly': False, 'name': 'ptui_loginuin', 'path': '/',
                 'secure': False, 'value': '1219964985'},
                {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/',
                 'secure': False, 'value': '584415353'},
                {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False,
                 'value': '4fd65c76325acf822b8b528a15f2a392297a3039452f348bfeb11c12a0fc3b62'},
                {'domain': '.work.weixin.qq.com', 'expiry': 1617273349, 'httpOnly': False, 'name': 'wwrtx.i18n_lan',
                 'path': '/', 'secure': False, 'value': 'zh'},
                {'domain': '.qq.com', 'expiry': 1677753346, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False,
                 'value': 'GA1.2.1688755123.1613182480'},
                {'domain': '.qq.com', 'expiry': 1897288226, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/',
                 'secure': False, 'value': '9ab0ba39789428ab'},
                {'domain': '.work.weixin.qq.com', 'expiry': 1641100817, 'httpOnly': False, 'name': 'wwrtx.c_gdpr',
                 'path': '/', 'secure': False, 'value': '0'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False,
                 'value': 'ThMZD0y5mw0-lQgOlJQPXUq6uWsnpLp9xwhDezLIT0U1AGzjmx9Cy1Ps_qQ1H9dm7pLCdxExWg2uOPZgXcnlS1sjyzb5xpepHnQrCwrw_dMRzzSl1253wcHbqetXdVB4yJVCn3udHNxYEQ4Zp9JeM0qQdSx-MwS4Vr0Ihx37xx19Np3DhT3-VO-SG-OnH8m4EBgGqP-Fvly5wx_WLDGkQcejEMuBMkEnhH2P5JapqqkpjvrcNPOscsAvVZUf8Sh4l9Sh7hcXpW2A7RogH2Ot_Q'},
                {'domain': '.work.weixin.qq.com', 'expiry': 1646217337, 'httpOnly': False,
                 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False,
                 'value': '1614647406,1614680471,1614681094,1614681337'},
                {'domain': '.qq.com', 'expiry': 1904971625, 'httpOnly': False, 'name': 'pac_uid', 'path': '/',
                 'secure': False, 'value': '1_584415353'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False,
                 'value': '1'},
                {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False,
                 'value': 'direct'},
                {'domain': '.qq.com', 'expiry': 1614681397, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False,
                 'value': '1'},
                {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False,
                 'value': 'n8KwV1gpaD'}]
            db = shelve.open("./mydbs/cookies")
            db['cookie'] = cookies
            db.close()

    然后再把文件读取出来

        def test_shelve(self):
            db = shelve.open("./mydbs/cookies")
            cookies = db['cookie']
            self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
            for cookie in cookies:
                if "expiry" in cookie.keys():
                    cookie.pop('expiry')
                self.driver.add_cookie(cookie)
            self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
            self.driver.find_element(By.CSS_SELECTOR, ".index_service_cnt_itemWrap:nth-child(2)").click()
            self.driver.find_element(By.ID, "js_upload_file_input").send_keys('D:outlooksmybook.xlsx')
            assert "mybook.xlsx" == self.driver.find_element(By.ID, "upload_file_name").text

    作业:

    使用cookie登录企业微信,完成导入联系人,加上断言验证

  • 相关阅读:
    【3.1】学习C++之再逢const
    【8】学习C++之this指针
    【7】学习C++之类的构造函数
    【6】学习C++之类的实例化及访问
    【5】学习C++之类的概念
    【4】学习C++之内存管理
    【3】学习C++之const关键字的使用
    【2】学习C++之引用
    【C#第一天】数据相关
    【1】学习C++时,一些零散知识点01
  • 原文地址:https://www.cnblogs.com/hantongxue/p/14429685.html
Copyright © 2020-2023  润新知