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

1. 复用已有的浏览器

开启chrome debug模式

chrome -remote-debugging-port=9222
--enable-logging --v=1
# Generated by Selenium IDE
from time import sleep

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


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:9222"
        self.driver = webdriver.Chrome(options=chrome_opts)

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

    def test_test(self):
        sleep(3)
        #self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.CSS_SELECTOR, ".index_service_cnt_item_title").click()

使用cookies

# Generated by Selenium IDE
import json
from time import sleep
from typing import List, Dict

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


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:9222"
        self.driver = webdriver.Chrome()
        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):
        # sleep(15)
        # 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.get("https://work.weixin.qq.com/wework_admin/frame#index")
        self.driver.find_element(By.CSS_SELECTOR, '.index_service_cnt_itemWrap').click()
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("hello")
        self.driver.find_element(By.XPATH, '//*[@id="memberAdd_acctid"]').send_keys("hello2")

作业

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

一、复用浏览器获取cookie
https://github.com/gqxjones/MyprojectHogwarts/blob/master/test_selenium/test_ChromeOptions.py

image
二、使用已有cookie获取网页并点击添加
https://github.com/gqxjones/MyprojectHogwarts/blob/master/test_selenium/test_cookies.py

image

import json
from typing import List, Dict

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


class TestCookies:
    def setup(self):
        # 设置 chromeoptions
        chrome_opts = webdriver.ChromeOptions()
        # 设置 chrome debugging 代理
        chrome_opts.debugger_address = '127.0.0.1:9222'
        self.driver = webdriver.Chrome(options=chrome_opts)
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)

    def test_cookies(self):
        """
        1. 配置 chrome 浏览器环境变量
        2. 启动 chrome debugging 时所有浏览器必须关闭
        3. cmd: Google\ Chrome -remote-debugging-port=9222
        4. 启动浏览器后扫码登录
        4. 执行用例并获取 cookies 
        """
        
        cookies = self.driver.get_cookies()
        with open('../../data/cookies.txt', 'w') as f:
            json.dump(cookies, f)
        with open('../../data/cookies.txt', 'r') as f:
            cookies: List[Dict] = json.load(f)
            print(cookies)
        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.ID,
                                 'username').send_keys('TEST')
        self.driver.find_element(By.ID,
                                 'memberAdd_acctid').send_keys('TEST')

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

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


class TestBrowserReuse:
    def setup_method(self, method):
        chrome_opts = webdriver.ChromeOptions()
        chrome_opts.debugger_address = "localhost:8083"
        # 调试模式下已登录
        self.driver = webdriver.Chrome(options=chrome_opts)
        self.driver.get("https://work.weixin.qq.com/")
        # 隐式等待
        self.driver.implicitly_wait(5)

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

    def test_weixin(self):
        # 获取复用浏览器的cookies
        cookies = self.driver.get_cookies()
        # 写入到文件
        with open("work_weixin_cookies", "w")as f:
            json.dump(cookies, f)
        # 读出到列表
        with open("work_weixin_cookies", "r")as f:
            cookies: list[tuple] = json.load(f)
            print(cookies)
        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, '[class="member_edit_cover_avatar"]').click()
        self.driver.find_element(By.XPATH, '//*[@class="ww_fileInput js_file"]').send_keys(
            'C:\\Users\\Lin\\Pictures\\him.jpg')
        # 点击保存图片
        self.driver.find_element(By.CSS_SELECTOR, '[class="qui_btn ww_btn ww_btn_Blue js_save"]').click()
        # 添加姓名
        self.driver.find_element(By.CSS_SELECTOR, '#username').send_keys("丁海寅")
        self.driver.find_element(By.CSS_SELECTOR, '[name=english_name]').send_keys("him")
        self.driver.find_element(By.CSS_SELECTOR, '[name=acctid]').send_keys('DingHaiYin')
        # 单选按钮点击
        self.driver.find_element(By.CSS_SELECTOR, '[value="1"]').click()
        self.driver.find_element(By.ID, "memberAdd_mail").send_keys("DingHaiYin@qq.com")
        self.driver.find_element(By.CSS_SELECTOR, '#memberAdd_title').send_keys("男朋友")
        # 点击保存
        self.driver.find_element(By.XPATH, '//*[@class="qui_btn ww_btn js_btn_save"]').click()
        sleep(5)

开启chrome debug模式

chrome -remote-debugging-port=9222

虽然感觉跟大家答案一样,但是还是提交一下
作业

虽然大家都一样,还是上传一段调试通了的代码 :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