【selenium实战1】selenium企业微信实战1

虽然大家都一样,还是上传一段调试通了的代码 :pleading_face: :pleading_face:

import json
from time import sleep

from selenium import webdriver
from selenium.webdriver.common.by import By

'''
selenium使用w3c规定的webdriver协议将命令发送出去
'''


class TestWecat:
    '''
    - 通过chrome调试模式命令:chrome -remote-debugging-port=9222,启动调试浏览器
    - 新建chrome参数options, 将本机调试端口参数传入,复用已有的浏览器(调试模式),直接跳过前面的步骤,进入关键步骤
    - 新建chrome浏览器,将配置好的options参数传入浏览器对象
    - 打开浏览器并最大化,设置隐式等待3秒
    - 通过调试模式可以直接进入登录好的状态,获取到cookie=[{"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "wwrtx.vid", "path": "/", "secure": true, "value": "1688850284528427"}, {"domain": ".work.weixin.qq.com", "httpOnly": true, "name": "wwrtx.refid", "path": "/", "secure": true, "value": "19475224753481799"}, {"domain": ".work.weixin.qq.com", "httpOnly": true, "name": "wwrtx.vst", "path": "/", "secure": true, "value": "BicSEKU8MEBfjOzwOqapeTdMJ8kM7CfOlgiPz75bKwjMhd7idn2Rbi-UoJ-2a6YDNVTHakno5EOpNVxEqUN1AFwj7TOvZSFUYCAK0E2n3VKeA8cL6yc4ka65yKxBFBIipTVkSQ_FPIutL5kAEOVdhWHheF_zc8JETDvXX_MvHJ1FdrL5ETaExjPKXvuIvxQlfSKTBz354SWLP7GSHDDcQrvUfDpfRtMDkGNnzeZzuWiET5x_BXiTPVKidyjnX8QcYctvTcht1319oMlGGgwZvQ"}, {"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "wxpay.vid", "path": "/", "secure": true, "value": "1688850284528427"}, {"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "wxpay.corpid", "path": "/", "secure": true, "value": "1970325123121219"}, {"domain": ".work.weixin.qq.com", "httpOnly": true, "name": "wwrtx.sid", "path": "/", "secure": true, "value": "wiij4h256w4FUQRhL9s5MWsHWYDk8PSPefXwgQi0F9p6R11EbJLQQ8s2Q1wG3tWD"}, {"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "Hm_lvt_9364e629af24cb52acc78b43e8c9f77d", "path": "/", "secure": true, "value": "1585656932,1585657951,1585657964,1585662318"}, {"domain": ".qq.com", "httpOnly": false, "name": "_gat", "path": "/", "secure": true, "value": "1"}, {"domain": ".work.weixin.qq.com", "httpOnly": true, "name": "wwrtx.ref", "path": "/", "secure": true, "value": "sites"}, {"domain": ".qq.com", "httpOnly": false, "name": "ptui_loginuin", "path": "/", "secure": true, "value": "476045231"}, {"domain": ".qq.com", "httpOnly": false, "name": "_ga", "path": "/", "secure": true, "value": "GA1.2.1136770173.1585656932"}, {"domain": ".work.weixin.qq.com", "expiry": 1588255487.050997, "httpOnly": false, "name": "wwrtx.i18n_lan", "path": "/", "secure": false, "value": "zh"}, {"domain": ".qq.com", "httpOnly": false, "name": "_gid", "path": "/", "secure": true, "value": "GA1.2.721303322.1585656932"}, {"domain": ".qq.com", "httpOnly": false, "name": "LW_sid", "path": "/", "secure": true, "value": "Z1H5z8Y1O5Q9P889A9c1N6N0y1"}, {"domain": ".qq.com", "httpOnly": false, "name": "ptcz", "path": "/", "secure": true, "value": "88b5b82ab2de9c9bcabb07cd4ba5ff4d786dd95fb62bf38ec198dce9ec87dd9e"}, {"domain": ".qq.com", "httpOnly": false, "name": "RK", "path": "/", "secure": true, "value": "Te5FSo9zfB"}, {"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "wwrtx.d2st", "path": "/", "secure": true, "value": "a8862074"}, {"domain": ".qq.com", "httpOnly": false, "name": "pgv_pvi", "path": "/", "secure": true, "value": "9316420608"}, {"domain": ".work.weixin.qq.com", "httpOnly": true, "name": "wwrtx.ltype", "path": "/", "secure": true, "value": "1"}, {"domain": ".qq.com", "httpOnly": false, "name": "pgv_pvid", "path": "/", "secure": true, "value": "5266567000"}, {"domain": ".work.weixin.qq.com", "httpOnly": false, "name": "wwrtx.logined", "path": "/", "secure": true, "value": "true"}, {"domain": ".qq.com", "httpOnly": false, "name": "eas_sid", "path": "/", "secure": true, "value": "A1w5u7a9k6U0E5H66611V2E8L5"}, {"domain": ".qq.com", "httpOnly": false, "name": "tvfe_boss_uuid", "path": "/", "secure": true, "value": "b159682f22708891"}, {"domain": ".qq.com", "httpOnly": false, "name": "LW_uid", "path": "/", "secure": true, "value": "L1N587W9x6K0I5X676K1g1G8X5"}]
    - 将cookie提取出来,通过json模块直接保存到cookies.txt文档文件中
    - 定位到添加成员按钮并点击,在下面的输入框输入内容

    - 如果在cookie生效时间内登录,那么可以直接读取cookies.txt文件,将其中的验证时间字段删掉,使用对应的cookie登录页面
    '''

    def setup(self):
        # webdriver其中一个参数
        chrome_options = webdriver.ChromeOptions()
        # 设置调试地址,需要与chromedebug地址相同
        chrome_options.debugger_address = '127.0.0.1:9222'
        # 将调试地址参数传入webdriver的参数中
        self.driver = webdriver.Chrome(options=chrome_options)
        # self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.implicitly_wait(3)

    def teardown(self):
        self.driver.quit()

    def test_wecat(self):
        # self.driver.get('https://work.weixin.qq.com/wework_admin/loginpage_wx?from=myhome')
        cookies = self.driver.get_cookies()
        # 将cookies提取出来后存到文档中
        with open('cookies.txt', 'w') as f:
            json.dump(cookies, f)
        # 从对应的文件中读出cookies
        '''
        cookie中的expiry字段保存了cookie的保存时间
        '''
        with open('cookies.txt', 'r') as f:
            cookies: list[dict] = json.load(f)
        for cookie in cookies:
            # 如果cookie验证时间字段在cookie中,则删掉
            if 'expiry' in cookie.keys():
                cookie.pop('expiry')
            # 在浏览器对象中依次添加取得的cookie
            self.driver.add_cookie(cookie)

        self.driver.find_element(By.CSS_SELECTOR, '.index_service_cnt_itemWrap').click()
        self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys('二娃')
        self.driver.find_element(By.CSS_SELECTOR, '[name=english_name]').send_keys("Steven_Tang")
        self.driver.find_element(By.CSS_SELECTOR, '#memberAdd_acctid').send_keys('Steven_Tang')
        # ………………此处省略若干字

以上

1 个赞

1、复用浏览器技术获取cookie

      进入debug模式:chrome -remote-debugging-port=9222
import json
from selenium import webdriver

class TestWeixin():
    def setup_method(self):
         chrome_dubble = webdriver.ChromeOptions()
        chrome_dubble.debugger_address = "127.0.0.1:9222"
        # 设置一个调试地址与端口
        self.driver = webdriver.Chrome(options=chrome_dubble)
        # 打开调试chrome
        self.driver.implicitly_wait(3)

    def test_weixin(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        cookies = self.driver.get_cookies()
        with open("cookies.txt",'w') as mm:
       # 生成一个cookies的TXT文件,并将获取的cookie写入到里面
            json.dump(cookies,mm)

2、使用获取的cookie,登入并点击添加成员

from selenium.webdriver.common.by import By
import json
from selenium import webdriver

class TestWeixin():
    def setup_method(self):

        self.driver = webdriver.Chrome()
        # 打开chrome
        self.driver.implicitly_wait(3)

    def test_weixin(self):
        self.driver.get("https://work.weixin.qq.com/")
        with open("cookies1.txt",'r') as login:
            # 读取文件中的数据
            COOKIE = json.load(login)
            cookies = COOKIE[0]
            # 因参数为列表中的字典故将字典数据取出

        self.driver.add_cookie(cookies)
        # 将传入的cookie加入到当前session
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.find_element(By.CSS_SELECTOR,'.index_service_cnt_itemWrap').click()
  1. 使用复用浏览器技术获取企业微信的cookie,点击添加成员
    https://github.com/XFbeibei/HogwartsLagoutesting/blob/master/selenium_testing/test_weixin.py
from time import sleep

import allure
import pytest
from selenium import webdriver


# @pytest.mark.skip
class TestTest():
    def setup_method(self, method):
        #声明一个变量,设置为chrometoptions
        chrome_opts=webdriver.ChromeOptions()
        #set address same as chrome debugging port
        chrome_opts.debugger_address="127.0.0.1:9998"
        self.driver = webdriver.Chrome(options=chrome_opts)
        # self.driver.get("https://work.weixin.qq.com/")
        self.driver.implicitly_wait(3)

    def teardown_method(self, method):
        self.driver.quit()

    def test_test(self):

        self.driver.find_element_by_xpath('//*[@id="_hmt_click"]/div[1]/div[4]/div[2]/a[1]/div/span[2]').click()

https://github.com/shexiaoheng/web_wechat_1

https://github.com/shexiaoheng/web_wechat_1/blob/master/webwechat/test_save_cookies.py

https://github.com/shexiaoheng/web_wechat_1/blob/master/webwechat/test_web_wechat.py

1、使用复用浏览器技术获取企业微信的cookie,点击添加成员
https://github.com/ZHHAYO/TestingDemo/blob/master/test_selenium/test_selenium.py

虽然交晚了,但还是要交一下作业的 :joy:

1、复用浏览器和使用

2、点击添加成员

复用浏览器获取企业微信cookie,点击添加成员

使用复用浏览器技术获取企业微信的cookie,点击添加成员

import json
from time import sleep
from typing import List, Dict

from selenium import webdriver
from selenium.webdriver.common.by import By


class TestCookie():
    def setup(self):
        # 声明一个变量,设置为chrometoptions
        chrome_opts = webdriver.ChromeOptions()
        chrome_opts.debugger_address = "127.0.0.1:9222"
        #options=chrome_opts
        self.driver = webdriver.Chrome()
        self.driver.maximize_window()
        self.driver.get("https://work.weixin.qq.com/")
        self.driver.implicitly_wait(3)

    def teardown(self):
        self.driver.quit()

    def test_cookies(self):

        # 获取cookie值
        # cookies = self.driver.get_cookies()
        # 打开txt文件并且把cookies变量的值以json的形式存入文件内
        # with open("cookies.txt", "w") as f:
        #   json.dump(cookies, f)
        #读取txt内的值,取出赋值给cookies
        with open("cookies.txt", "r") as f:
            cookies: List[Dict] = json.load(f)

        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').click()
        self.driver.find_element(By.CSS_SELECTOR,'#username').send_keys("1_name")
        self.driver.find_element(By.XPATH,'//*[@id="memberAdd_english_name"]').send_keys("2_name")
        sleep(3)


https://github.com/lulihui12/selenium-homework/blob/master/test_selenium.py

使用复用浏览器技术获取企业微信的cookie,点击添加成员
'''
import json
from typing import List, Dict

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By


class Testchat:
    def setup(self):
        # 声明一个变量,设置为chromeoptions
        chrome_opts = webdriver.ChromeOptions()
        # 设置chrome debugging代理,端口号保持一致 :Google\ Chrome -remote-debugging-port=9222
        chrome_opts.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=chrome_opts)
        self.driver.get("https://work.weixin.qq.com/")
        # 隐式等待
        self.driver.implicitly_wait(3)

    def teardown(self):
        self.driver.quit()

    def test_chat(self):
        # 获取cookies并给到变量
        cookies = self.driver.get_cookies()
        # 保存到文件  注:open之后,才可以读写
        with open("cookies.txt", 'w') as f:
            json.dump(cookies, f)
        # 读取文件
        with open("cookies.txt", 'r') as f:
            cookies: List[Dict] = json.load(f)
        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_item_title').click()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("tester")
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_acctid").send_keys('12323232323')
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_phone").send_keys('13381037751')
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_telephone").send_keys('021-9898383')
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_mail").send_keys('12343555@qq.com')
        self.driver.find_element(By.CSS_SELECTOR, "#memberEdit_address").send_keys("北京海淀的呀呀呀呀呀")
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_title").send_keys("技术总监")

https://github.com/Liyb0326/LagouProject/tree/test/selenium1

作业地址

page:
https://github.com/mengqiD11/selenium_3/blob/master/page/base_page.py
https://github.com/mengqiD11/selenium_3/blob/master/page/add_member.py
https://github.com/mengqiD11/selenium_3/blob/master/page/index.py
testcases:
https://github.com/mengqiD11/selenium_3/blob/master/testcase/test_add_member.py

作业地址

作业:https://github.com/606keng/weeds_study/blob/master/selenium_study/qiyeweixin/test_cookies.py

https://github.com/tim8709/hogwarts_testing/blob/master/web/debug_and_cookies.py

https://github.com/Ashley-luo66/hogwarts_lagou_homework/tree/master/selenium_homework/selenium_1

使用复用浏览器技术获取企业微信的cookie,点击添加成员

class Test_debug():
    def setup_method(self):
        #使用网页复用
        chrome_opts=webdriver.ChromeOptions()
        chrome_opts.debugger_address="127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=chrome_opts)
        self.driver.implicitly_wait(3)

    def teardown_method(self):
        self.driver.quit()

    def test_1(self):
        #获得cookies
        cookies=self.driver.get_cookies()
        with open("cookies.txt", 'w') as f:
             json.dump(cookies, f)
        with open("cookies.txt", "r") as f:
            cookies:List[Dict]=json.load(f)
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            self.driver.add_cookie(cookie)
        #点击添加成员
        self.driver.find_element(By.CSS_SELECTOR,'.index_service_cnt_itemWrap:nth-child(1)').click()