企业微信的自动化登陆1

课后调查表:https://hogwarts.jinshuju.com/f/kLcBWZ

# Generated by Selenium IDE
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class Test():
    def setup_method(self, method):
        options = Options()
        #和浏览器打开的调试端口进行通信
        #浏览器要使用 --remote-debugging-port=9222 开启调试
        options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()

    def test_(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
        self.driver.find_element(By.ID, "su").click()
        time.sleep(2)
        self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

使用 cookie

# Generated by Selenium IDE
import shelve
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class Test():
    def setup_method(self, method):
        chrome_options = Options()
        #和浏览器打开的调试端口进行通信
        #浏览器要使用 --remote-debugging-port=9222 开启调试
        chrome_options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()

    def test_baidu(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
        self.driver.find_element(By.ID, "su").click()
        time.sleep(2)
        self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_wework(self):
        self.driver.get("https://work.weixin.qq.com/")
        # 创建或者打开一个数据库
        db = shelve.open("cookies")
        #cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'QggRK2Smqd5_lH6k6RDLimOx5yJ2Lfg5Gf8irncrB648gbArqGhg3bPA5k_NlXPF'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a5484050'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688850888546920'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688850888546920'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325091099135'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '16764371053223907'}, {'domain': 'work.weixin.qq.com', 'expiry': 1594932599, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '2plvpn'}, {'domain': '.work.weixin.qq.com', 'expiry': 1594932599, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597497421, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'exywTkwmcl'}, {'domain': '.qq.com', 'expiry': 1597489128, 'httpOnly': False, 'name': 'lskey', 'path': '/', 'secure': False, 'value': '00010000db2ca3a4bb964b0eed65356b900e1beae1f7a34e9e1b87fec2788e87760b807c4cfa59f7f489949c'}, {'domain': '.qq.com', 'expiry': 1596428506, 'httpOnly': False, 'name': 'ptui_loginuin', 'path': '/', 'secure': False, 'value': '742976679'}, {'domain': '.qq.com', 'expiry': 1656768418, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1486340881.1579059579'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/', 'secure': False, 'value': '1256305343'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': '21184e650f6a6683b1089473bd27234cc787221ed0761e762ef7c706c95c1153'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'SSvtaG0HPX3LrumGQo-0d8z8F9NDIt2-SJar2k4JleK8PcHpjPYXXt2qyrgbY5zCAiTy4KEuHlMTID5PRPO-ibY9A298-8R7Rg05GZ0CvuGsu_LU2mYtkkbpM1kHBlNrdFf1zbkZlOQ4P6Y35tIelk5ewe1ePJ9zXUcDsxuTVU-9Ocwk4YT31_gwl4WtraACGf4-ngdcxZ4GrTw3s5b9g5ktpP6kg9bi1sL_noxgoRxxowU9YO2pGe1X8_oKqXBVQmrSNTuQObkteGYkLqmI_w'}, {'domain': '.qq.com', 'expiry': 1597489128, 'httpOnly': False, 'name': 'luin', 'path': '/', 'secure': False, 'value': 'o1256305343'}, {'domain': '.qq.com', 'expiry': 1897608032, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': 'db5af6172ed41f4b'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '535707648'}, {'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': '.work.weixin.qq.com', 'expiry': 1625229244, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1591409096,1593693242'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '5034123288'}]
        # 将数据存储到 shelve 中
        #db["cookies"] = cookies
        # 取出数据
        cookies = db["cookies"]
        #获取 cookies
        #print(self.driver.get_cookies())
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            # 把字典加入到 driver 的 cookie 中
            self.driver.add_cookie(cookie)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.find_element(By.XPATH, "//*[@id='menu_contacts']").click()
        db.close()

作业

完成课上调试代码,将代码贴到下方评论区

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

class TestDebug:
def setup_method(self, method):
options = Options()
options.debugger_address = “127.0.0.1:7157”
self.driver = webdriver.Chrome(chrome_options=options)

def test_baidu(self):
    self.driver.get("https://www.baidu.com/")
    self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
    self.driver.find_element(By.ID, "su").click()
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class TestWework:
    def setup(self):
        # options=Options()
        # options.debugger_address="127.0.0.1:9222"
        # self.driver=webdriver.Chrome(options=options)
        self.driver = webdriver.Chrome()

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

    def test_hogwarts(self):
        self.driver.get('http://www.baidu.com')
        self.driver.find_element(By.CSS_SELECTOR,'#kw').send_keys('hogwarts')
        self.driver.find_element(By.CSS_SELECTOR,'#su').click()

    def test_cookie(self):
        self.driver.get("https://work.weixin.qq.com/")
        # cookies=self.driver.get_cookies()
        cookies=[{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594956757'}, {'domain': '.qq.com', 'expiry': 1594956772, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626492756, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594901170,1594910118,1594956128,1594956379'}, {'domain': '.qq.com', 'expiry': 1908270771, 'httpOnly': False, 'name': 'pac_uid', 'path': '/', 'secure': False, 'value': '0_a2c7b844f1395'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'wffsk1kSMbKgXP72ZN5Wy_uvqT2ykhFWkDXjKR6TddFLDtZgkGZ6LsJAY1OtuAI7c-mZPOvQZlQJYQRXbhVxD-F-NYB9fNEt5e_KXB9YxZw4nlFMZ0uXCUw7tvjMsND9zOVQMIJK5wEKMz8gw5seXrEJx81WJze_lWo1LuNew5Xt9XoBkpeWQVch-1hM6w7JKdzo_Q8w0-ZZtvuH1UiQDpScmxqluXoSeHPNtxWrmQ6XA4EHV-C1458DZemlcWaPIqTfWoJbnnlUYxeLW8mtAw'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688853426861743'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '5874400720'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '6742009856'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970324973142954'}, {'domain': '.qq.com', 'expiry': 1595043112, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.771674355.1594901173'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'sA39ulu4BBqPk53lakNycLfkP1LeHBlPIWtHwNa6Jb4MgZZOdt1zphvzdQ4Gazgs'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a928727'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '2304051824132782'}, {'domain': '.qq.com', 'expiry': 1907580518, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': '87caebff68eab743'}, {'domain': '.work.weixin.qq.com', 'expiry': 1594987662, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1594965874, 'httpOnly': False, 'name': 'pt_local_token', 'path': '/', 'secure': False, 'value': '123456789'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597548756, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.qq.com', 'expiry': 1658028712, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.811375496.1593949239'}, {'domain': 'work.weixin.qq.com', 'expiry': 1594987662, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '8hua9vm'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688853426861743'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 1908270771, 'httpOnly': False, 'name': 'iip', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': '1b7b23c472e945ab697d9fd569132a5084eefe0bcfb08143689a9046ba30ff7d'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'jeBJF89Sad'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}]
        print(cookies)
        for cookie in cookies:
            if 'expiry' in cookie.keys():
                cookie.pop('expiry')
            self.driver.add_cookie(cookie)     #======  cookies injection 
        self.driver.refresh()
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
        self.driver.find_element(By.CSS_SELECTOR,'#menu_contacts').click()



课后作业

import shelve
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

class TestTestclass():
  def setup_method(self, method):
    chromenew = Options()
    chromenew.debugger_address = "127.0.0.1:9222"
    self.driver = webdriver.Chrome(options=chromenew)
    self.vars = {}
  
  def test_testclass(self):
    self.driver.get("https://www.baidu.com/index.php?tn=monline_3_dg")
    self.driver.set_window_size(1295, 695)
    self.driver.find_element(By.ID, "kw").click()
    self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹学院")
    self.driver.find_element(By.ID, "kw").send_keys(Keys.ENTER)
    self.vars["window_handles"] = self.driver.window_handles
    time.sleep(2)
    self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()


  def test_wework(self):
    self.driver.get('https://work.weixin.qq.com/')
    ##先获取到cookies,然后去掉有效期,将cookies存储到db数据库里
    #print(self.driver.get_cookies())
    #打开数据库
    db = shelve.open('cookies')
    #进行存储
    #db["cookies"] =cookies
    #读取cookies
    cookies = db["cookies"]
    for cookie in cookies:
      if "expiry" in cookie.keys():
        cookie.pop("expiry")
        #把字典加入到driver的cookies中
        self.driver.add_cookie(cookie)
    self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
    self.driver.find_element(By.ID,'menu_contacts').click()
    #关闭数据库
    db.close()

https://github.com/muzi1218/hogwarts/blob/master/web/login_weixin.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from time import sleep
import pytest
import shelve


class Test:

    def setup(self):
        option = Options()
        # 浏览器打开调试端口(cmd输入:chrome --remote-debugging-port=9999)
        option.debugger_address = "127.0.0.1:9999"
        # 通过开启的调试端口对浏览器进行复用(避免每次重新启动一个新的浏览器窗口)
        # self.driver = webdriver.Chrome(options=option)
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)

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

    @pytest.mark.skip
    def test_baidu(self):
        self.driver.get("https://www.baidu.com")
        self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院\n")
        self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_wework(self):
        self.driver.get("https://work.weixin.qq.com")
        # 浏览器复用打开企业微信后,扫码登陆企业微信获取cookies并打印在控制台
        # print(self.driver.get_cookies())
        # 从pycharm控制台复制获取的cookies
        # cookies = [{*:*}, {*:*}]
        # 创建一个“cookies”数据库
        db = shelve.open("cookies")
        # 将cookies存入数据库db["cookies"]
        # db["cookies"] = cookies
        for cookie in db["cookies"]:
            # 删除登录cookies内的expiry,实际验证不删除也能登录成功
            # if "expiry" in cookie.keys():
            #     cookie.pop("expiry")
            self.driver.add_cookie(cookie)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 关闭数据库
        db.close()
from selenium import webdriver
from time import sleep
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.chrome.options import Options
import shelve

class Test():
    def setup_method(self,method):
        options = Options()
        #和浏览器打开的调试端口进行通信
        #浏览器要使用 --remote-debugging-port=9222 开启调试
        options.debugger_address = "127.0.0.1:9222"
        self.drivers = webdriver.Chrome()

    def test_wework(self):
        # print(self.drivers.get_cookies())
        self.drivers.get("https://work.weixin.qq.com/")
        #创建或者打开一个数据库
        db = shelve.open("cookies")
        # cookies =db["cookies"]
        # cookies= [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a6442398'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': True, 'value': '1594991174'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688852958815230'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595022709, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': True, 'value': '34k8011'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688852958815230'}, {'domain': '.qq.com', 'httpOnly': False, 'name': 'pgv_info', 'path': '/', 'secure': True, 'value': 'ssid=s2105101410'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325122155175'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595022709, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '34k8011'}, {'domain': '.qq.com', 'expiry': 1595078522, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.429581134.1594991176'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': True, 'value': '3079379597'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': True, 'value': '0373123'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': True, 'value': 'TTMXOgLHwPDnJCiGab0KtCWp0BH1acEw1g3_VU8uxdexytdd9pohdrRCBbUwXf1q_dpjXu2DLZIrUeWQbvS-u457WnzphMXc7Xsp9z5jfXVa9j03OaVNkFxhV98KJereHMRXXIhKF9MaAds-_SmVBzJOd0hE5l-7k-3WXE152U5xTWEIf7cL027UVYzCAfLY1pSUlF3qdjRb-C69dJtc1FakueYjVEJ1F530oqO3_2Ir1Tq4s6RtLkfFW97MZ3zvXw7cECnEbEDx6YBDBEdSqA'}, {'domain': '.qq.com', 'expiry': 1658064122, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.201909767.1594991176'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': True, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'CJ21X6Ifhld28od0UjW1WC734eEjAwCY9x_xWOX-XGgZJGGd6xLsKNF6qMvS-TMd'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595022709, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': True, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626527173, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': True, 'value': '1594991174'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597584125, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}]    #     #获取cookie
        #酱数据存到shelve中
        # db["cookies"] = cookies
        #读取cookies
        cookies = db["cookies"]
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            #把字典加入到driver的 cookie 中
            self.drivers.add_cookie(cookie)
        self.drivers.get("https://work.weixin.qq.com/wework_admin/frame")
        db.close()

步驟:

1.登錄企業微信,獲取cookies
2.刪除cookies中的expiry字段
3.把cookies加入到driver中
4.找到“通訊錄”元素並點擊,驗證已經登錄成功
步驟一
【前提:1.打開企業微信登錄頁,2.掃碼登錄企業微信,3.運行下列代碼復用當前頁面點擊“通訊錄”獲取cookies】

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class TestCookie:

    def setup(self):
        option = Options()
        option.debugger_address = "127.0.0.1:5123"
        self.driver= webdriver.Chrome(options=option)

    def test_cookie(self):
        #1.獲取cookies
        self.driver.find_element_by_css_selector("#menu_contacts>span").click()
        print(self.driver.get_cookies())


步驟二
賦值變量並處理cookies

class TestCookie:

    def setup(self):
        option = Options()
        option.debugger_address = "127.0.0.1:5123"
        self.driver= webdriver.Chrome()
        # self.driver= webdriver.Chrome(options=option) 注釋,無需復用,打開新窗口即可

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

    def test_cookie(self):

        self.driver.get("https://work.weixin.qq.com/") #打開企業微信首頁
        #2.處理cookies,刪除cookies中的expiry字段
        cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a143792'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'VC5iNZi9cnKeRGXShsMcaPjnzXjl7JZXZAq7q-qpmN6Anhelbr4hdJjhJxi7kwR6AGcU98f4R62aoJXuS68QVIcmv1qyAcbOjKU3kHcjACNrf_mhqOcsOPpbNyPjfa88p7UxlxWe9UqBHzF93E2_RkiONHfifFoy8mv-6g6Sp08bqUpkRbllNGJoJKBRSWF60sknmzxXY1bxcj9yHGoWMZ8a0I9Q_RJv0Isic5tBmnhE6yD6eOzGMOX_1urpLzpeCEmfG0JH8K4fz2sy9hYSgA'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688850078090809'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688850078090809'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325013143892'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594991785'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '31994766012436178'}, {'domain': '.qq.com', 'expiry': 2147483646.75428, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': '812f15b852e3d78278bb064abd7b88b7234da881603761524587c69f61a39b8a'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626527784, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594948046,1594989661,1594991785'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '3113682960'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.qq.com', 'expiry': 2147483646.754136, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'ALq9+qcTQ6'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595021171.661933, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1595036847, 'httpOnly': False, 'name': '_qpsvr_localtk', 'path': '/', 'secure': False, 'value': '1594950447087'}, {'domain': '.qq.com', 'expiry': 1658063805, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.102308414.1594948046'}, {'domain': '.qq.com', 'expiry': 1594991845, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '3927141376'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'loW1XXHFt4TqZhdOqJRve17eC82UQXBNNAEM1edRc58-nIkDMEUK_vX-4edHCrbc'}, {'domain': '.qq.com', 'httpOnly': False, 'name': 'pgv_info', 'path': '/', 'secure': False, 'value': 'ssid=s8077261500'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595021171.661991, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '3poa175'}, {'domain': '.qq.com', 'expiry': 1595078205, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.1964484101.1594948046'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597583808.135545, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}]
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")

        #3.把cookies加入到driver中
            self.driver.add_cookie(cookie)
        # self.driver.refresh()  #這裏企業微信不能用刷新頁面(刷新后還是沒登錄狀態),而是直接打開登錄后的頁面
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        #4.找到“通訊錄”元素並點擊,驗證已經登錄成功
        self.driver.find_element_by_css_selector("#menu_contacts>span").click()

import shelve
import time

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class TestSele():

    def setup(self):
        chrome_options = Options()
        chrome_options.debugger_address = '127.0.0.1:7777'
        self.driver=webdriver.Chrome()
        self.driver.implicitly_wait(5)

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

    def test_selenium(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        db=shelve.open("cookies")
        cookies=db["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_id('menu_contacts').click()
        time.sleep(2)

import shelve
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
# 课堂链接https://ceshiren.com/t/topic/3832

class Test():
  def setup_method(self, method):
    # 加一个debug的选项
    chrome_options = Options()
    # 和浏览器打开的调试端口进行通信
    # 浏览器要使用 --remote-debugging-port=9222 开启调试
    chrome_options.debugger_address = "127.0.0.1:9222"
    # chrome传入复用参数,没有传入的话,会导致执行时重新打开一个新的浏览器窗口
    #self.driver = webdriver.Chrome(options=chrome_options)
    self.driver = webdriver.Chrome()

  def test_baidu(self):
    self.driver.get("https://www.baidu.com/")
    self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
    self.driver.find_element(By.ID, "su").click()
    # 避免录制操作的时候,太快,导致执行过程中找不到元素
    time.sleep(2)
    self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

  def test_wework(self):
    # 首页menu_index  通讯录menu_contacts
    # self.driver.find_element(By.ID,"menu_contacts").click()
    # 获取当前页面的 cookie 操作
    #print(self.driver.get_cookies())
    # 取消复用操作,需要打开一个新的网站/登录首页
    self.driver.get("https://work.weixin.qq.com/")
    # 创建或者打开一个数据库
    db = shelve.open("cookies")
    #cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a1237438'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595005136'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688851902808510'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688851902808510'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'CUg-F7NRTzbm2EKNHZtLwdM9bDfYt-Oo_FCjOz5ag9dIxzeNluS37YpBwuCL3Fzq'}, {'domain': '.qq.com', 'expiry': 1595092880, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.927606796.1595003246'}, {'domain': '.qq.com', 'expiry': 1852444088, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': '83ef318295ebc576'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595034779.699756, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1658078480, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.2070883560.1594901079'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597598482.909168, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'GdL0xykZQ1FTrEbrUHxA4N-e4rf0iYAguPhkJSZW7u5Tk4ZhnVdIdmMzzPKa1BONTgtCN2i57DfGqXsx0mhIRcpdJrK3CNmYZpjSXJ6vdsoRf9EUoqgohVEfAoa2MsDmX0zAGFWur5rk_B5byIp3sXU79AKeOK910aaHltsDjDJptGJMPYaImqSe3oJP9g_mSzcnON5IESJkFdbzYecTbvnjcxhY6zBBGRiIDcdCOQBAlm09i6V58nBEkyIQG4BVpDX1qos-RgE9NId_8auU3A'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626541136, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594901077,1595003245,1595005136'}, {'domain': '.qq.com', 'expiry': 1909226000.178051, 'httpOnly': False, 'name': 'pac_uid', 'path': '/', 'secure': False, 'value': '1_2276512907'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '39751613711652540'}, {'domain': '.qq.com', 'expiry': 1607328369.574417, 'httpOnly': False, 'name': 'ied_qq', 'path': '/', 'secure': False, 'value': 'o3422811267'}, {'domain': '.qq.com', 'expiry': 1601044376, 'httpOnly': False, 'name': 'eas_sid', 'path': '/', 'secure': False, 'value': 'j12526Q9x5s0I8t3s7U6H8J1u7'}, {'domain': '.qq.com', 'expiry': 1619508772, 'httpOnly': False, 'name': 'AMCV_248F210755B762187F000101%40AdobeOrg', 'path': '/', 'secure': False, 'value': '-1891778711%7CMCIDTS%7C18014%7CMCMID%7C64715027588967099733982594151805291151%7CMCAAMLH-1556955168%7C11%7CMCAAMB-1556955168%7CRKhpRz8krg2tLO6pguXWp5olkAcUniQYPHaMWWgdJ3xzPWQmdj0y%7CMCOPTOUT-1556357568s%7CNONE%7CMCAID%7CNONE%7CvVersion%7C2.4.0'}, {'domain': '.qq.com', 'expiry': 3685891366.728609, 'httpOnly': False, 'name': 'LW_pid', 'path': '/', 'secure': False, 'value': 'f25ae7b79fcadc45647fbede9e83287f'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325139144381'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '5518718503'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '930997248'}, {'domain': '.qq.com', 'expiry': 3685891366.728608, 'httpOnly': False, 'name': 'LW_PsKey', 'path': '/', 'secure': False, 'value': '380cb19771c4aa49be02048a5f20f91a'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595034779.699699, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '3ncltgs'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 3685891366.728607, 'httpOnly': False, 'name': 'LW_TS', 'path': '/', 'secure': False, 'value': '1538407713'}, {'domain': '.qq.com', 'expiry': 2147483643.549897, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': 'addcf9d7c1026698ce5df8b0f1a33aaa37819bf00a3a69295dc04ac60cb2c85d'}, {'domain': '.qq.com', 'expiry': 3685891366.345608, 'httpOnly': False, 'name': 'ue_uk', 'path': '/', 'secure': False, 'value': 'e1763e3761e8d802bf3b46af3d55af20'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/', 'secure': False, 'value': '2276512907'}, {'domain': '.qq.com', 'expiry': 3685891366.345609, 'httpOnly': False, 'name': 'ue_skey', 'path': '/', 'secure': False, 'value': '9e0e13f33ce1312412a7c1757e8a5369'}, {'domain': '.qq.com', 'expiry': 3685891366.345607, 'httpOnly': False, 'name': 'ue_ts', 'path': '/', 'secure': False, 'value': '1538407713'}, {'domain': '.qq.com', 'expiry': 3684674911.668788, 'httpOnly': False, 'name': 'ue_uid', 'path': '/', 'secure': False, 'value': '35a1c3909ef383fb6f21e0893fe54a30'}, {'domain': '.qq.com', 'expiry': 2147483646.689178, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'ZLiRGTWklB'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}]

    # 将数据存储到 shelve 中
    #db["cookies"] = cookies
    # 取出数据
    cookies = db["cookies"]
    #db.close()

    for cookie in cookies:
        if "expiry" in cookie.keys():
          cookie.pop("expiry")
        # 把字典加入到 driver 的 cookie 中
        self.driver.add_cookie(cookie)
    # 完成cookie的注入,就可以取消复用操作了
    self.driver.get("https://work.weixin.qq.com/wework_admin/frame#contacts")
    # 当显示登录状态过期,请重新登录;1、找出管理cookie失效的字段expiry,把该字典删掉;2、也有可能访问页面不对,需要重新手动登陆打印获取cookie
    #         if "expiry" in cookie.keys():
    #           cookie.pop("expiry")

浏览器复用

from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options


class TestWorkWeiXin:
    # 浏览器复用:
    # 关闭浏览器后命令行输入:chrome - -remote - debugging - port = 9222
    def setup(self):
        chrome_options = Options()
        chrome_options.debugger_address = '127.0.0.1:9222'
        self.driver = webdriver.Chrome(options=chrome_options)
        # self.driver.maximize_window()

    def test_address(self):
        self.driver.get('https://work.weixin.qq.com/')
        sleep(2)
        self.driver.find_element_by_css_selector(
            '#indexTop > div.index_top_inside > aside > a.index_top_operation_loginBtn.one-pan-link-mark').click()
        sleep(2)
        self.driver.find_element_by_css_selector('#menu_contacts > span').click()
        sleep(3)

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

使用cookie登录

import shelve

from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


# 使用cookie方式操作
class TestCookies():
    def setup(self):
        # 复用浏览器获取cookie
        chrome_options = Options()
        # chrome_options.debugger_address = '127.0.0.1:9222'
        # self.driver = webdriver.Chrome(options=chrome_options)
        self.driver = webdriver.Chrome()
        self.driver.get("https://work.weixin.qq.com/")
        self.driver.maximize_window()

    # 获取cookies并存储到shelve数据库中
    # def test_get_cookies(self):
    #     # 获取cookies
    #     get_cookies = self.driver.get_cookies()
    #     # 创建或者打开一个数据库
    #     db = shelve.open("./data/cookies")
    #     # 将数据存储到 shelve 中
    #     db["./data/cookies"] = get_cookies
    #     print(db["./data/cookies"])

    def test_cookie(self):

        # 取出数据
        db = shelve.open("./data/cookies")
        cookies = db["./data/cookies"]
        # # 打印 cookie
        print(cookies)
        # 把字典加入到 driver 的 cookie 中
        for cookie in cookies:
            # 处理”ecpiry“键值已防止cookie过期
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            self.driver.add_cookie(cookie)
        # 企业微信操作部分
        self.driver.find_element_by_css_selector(
            '#indexTop > div.index_top_inside > aside > a.index_top_operation_loginBtn').click()
        sleep(2)
        self.driver.find_element_by_css_selector('#menu_contacts > span').click()
        sleep(3)
        db.close()

    def teardown(self):
        self.driver.quit()
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 Test():
  def setup_method(self, method):
    options = Options()
    #和浏览器打开的调试窗口进行通信
    #浏览器要使用--remote-debugging-port=9222
    options.debugger_address = "127.0.0.1:9222"
    self.driver = webdriver.Chrome(options=options)

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

  def test_baidu(self):
    self.driver.get("https://www.baidu.com/")
    self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
    self.driver.find_element(By.ID, "su").click()
    time.sleep(2)
    self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

  def test_wework(self):
    self.driver.get("https://work.weixin.qq.com/")
    #创建或者打开一个数据库
    db = shelve.open("cookies")
    # cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595056046'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a6499289'}, {'domain': '.qq.com', 'expiry': 1595056066, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688851936808918'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688851936808918'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '5124164772250655'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'kd0GumIO-aLYbx155IAkwtEpa3_rRobjMJEV7j0LAxzbdbGedAUxdfC3SokVyeW0'}, {'domain': '.qq.com', 'expiry': 1595142438, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.401794814.1595049362'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595080897, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '34vqqr4'}, {'domain': '.qq.com', 'expiry': 1895578723, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': '60f43413a05feb99'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595080897, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1658128038, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1768954037.1594901102'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597648046, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.qq.com', 'expiry': 1597074957, 'httpOnly': False, 'name': 'ptui_loginuin', 'path': '/', 'secure': False, 'value': '1442678679'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqaccess_token', 'path': '/', 'secure': False, 'value': '72D7BE0F4ECB885DDC9A3FF9FFEC27B1'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqunionid', 'path': '/', 'secure': False, 'value': '2625521B3E5E33D13FF20E643A2B73EB'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_access_token_expiresAt', 'path': '/', 'secure': False, 'value': '1595575034'}, {'domain': '.qq.com', 'expiry': 2147483649, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 't2ypjGf4FA'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqrefresh_token', 'path': '/', 'secure': False, 'value': '07C5A2E5D285B4B75547FD3D2C95C828'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': 'f6a92c7c1c8a3ff3e1b0d3e1a17d25ab2bc707e8b0dd45d905a3a1883ac8a5d0'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/', 'secure': False, 'value': '1442678679'}, {'domain': '.qq.com', 'expiry': 1900767406, 'httpOnly': False, 'name': 'mobileUV', 'path': '/', 'secure': False, 'value': '1_17121a532ad_ff50'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqopenid', 'path': '/', 'secure': False, 'value': '16D0945258C9DA995660B19CB1B75859'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970324976151351'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '24604804'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '1709671424'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'jjT2_0ykGb4VguvyjVvx1QUElFMtZ-ad5KlUnGVTHduZQl0o_wWIPyJRd3ZMdmjmM0afH95xg9B9lIdjpts79k0fuNysvjLFp33ZVve48c_8xbjk3MDJKR1sxDMuf1MF3PpB0pCxY-raMCWVUIiMvlpGEdCtPKZrErAtE6bKVsCBCHtlSo05D5OiJdO0sVNPAGY2pGdKrCN9Tr51mq_3X4VCKHqPFbK9QqdCQSozObKULZaYOTUg7VgczRirOuX2v0jj48EpLPmR1WXKX_V7Bg'}, {'domain': '.qq.com', 'expiry': 1892378384, 'httpOnly': False, 'name': 'pac_uid', 'path': '/', 'secure': False, 'value': '0_5dff641052650'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626592046, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594901114,1595054255'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}]
    #将数据存储到shelve中
    # db["cookies"] = cookies
    #取出数据
    cookies = db["cookies"]
    #获取cookies
    # cookies = self.driver.get_cookies()
    # print(cookies)
    #cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595056046'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a6499289'}, {'domain': '.qq.com', 'expiry': 1595056066, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688851936808918'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688851936808918'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '5124164772250655'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'kd0GumIO-aLYbx155IAkwtEpa3_rRobjMJEV7j0LAxzbdbGedAUxdfC3SokVyeW0'}, {'domain': '.qq.com', 'expiry': 1595142438, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.401794814.1595049362'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595080897, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '34vqqr4'}, {'domain': '.qq.com', 'expiry': 1895578723, 'httpOnly': False, 'name': 'tvfe_boss_uuid', 'path': '/', 'secure': False, 'value': '60f43413a05feb99'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595080897, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1658128038, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1768954037.1594901102'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597648046, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.qq.com', 'expiry': 1597074957, 'httpOnly': False, 'name': 'ptui_loginuin', 'path': '/', 'secure': False, 'value': '1442678679'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqaccess_token', 'path': '/', 'secure': False, 'value': '72D7BE0F4ECB885DDC9A3FF9FFEC27B1'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqunionid', 'path': '/', 'secure': False, 'value': '2625521B3E5E33D13FF20E643A2B73EB'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_access_token_expiresAt', 'path': '/', 'secure': False, 'value': '1595575034'}, {'domain': '.qq.com', 'expiry': 2147483649, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 't2ypjGf4FA'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqrefresh_token', 'path': '/', 'secure': False, 'value': '07C5A2E5D285B4B75547FD3D2C95C828'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': 'f6a92c7c1c8a3ff3e1b0d3e1a17d25ab2bc707e8b0dd45d905a3a1883ac8a5d0'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'o_cookie', 'path': '/', 'secure': False, 'value': '1442678679'}, {'domain': '.qq.com', 'expiry': 1900767406, 'httpOnly': False, 'name': 'mobileUV', 'path': '/', 'secure': False, 'value': '1_17121a532ad_ff50'}, {'domain': '.qq.com', 'expiry': 1595575034, 'httpOnly': False, 'name': 'psrf_qqopenid', 'path': '/', 'secure': False, 'value': '16D0945258C9DA995660B19CB1B75859'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970324976151351'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '24604804'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '1709671424'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'jjT2_0ykGb4VguvyjVvx1QUElFMtZ-ad5KlUnGVTHduZQl0o_wWIPyJRd3ZMdmjmM0afH95xg9B9lIdjpts79k0fuNysvjLFp33ZVve48c_8xbjk3MDJKR1sxDMuf1MF3PpB0pCxY-raMCWVUIiMvlpGEdCtPKZrErAtE6bKVsCBCHtlSo05D5OiJdO0sVNPAGY2pGdKrCN9Tr51mq_3X4VCKHqPFbK9QqdCQSozObKULZaYOTUg7VgczRirOuX2v0jj48EpLPmR1WXKX_V7Bg'}, {'domain': '.qq.com', 'expiry': 1892378384, 'httpOnly': False, 'name': 'pac_uid', 'path': '/', 'secure': False, 'value': '0_5dff641052650'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626592046, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594901114,1595054255'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}]
    for cookie in cookies:
      if "expiry" in  cookie.keys():
        cookie.pop("expiry")
      #把字典加入到driver的cookie中
      self.driver.add_cookie(cookie)
    self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
    self.driver.find_element(By.ID, "menu_contacts").click()
    db.close()

课后作业

附带疑问

为什么我登录运行成功了但是,界面没有跳转过去呢?是需要扫描特殊的操作吗?

# -*- coding: utf-8 -*-
# @Pjname ;SubstituteBrush
# @Time   :2020/07/18/16:47
# @Author :Yuye
# @File   :test_Zd_login.py

import time
import pytest
import shelve
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class Testceshiren:
    def setup_method(self):
        self.Baseurl = "https://www.baidu.com/"
        self.BaseUrl = "https://work.weixin.qq.com/"
        chrome_options = Options()
        # 和浏览器打开的调试端口进行通信
        # 浏览器要使用 --remote-debugging-port=9222 开启调试
        chrome_options.debugger_address = "127.0.0.1:9191"
        self.driver = webdriver.Chrome()

    def tardown_method(self):
        pass

    def test_baidu(self):
        self.driver.get(self.Baseurl)
        self.driver.implicitly_wait(10)
        self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹学院")
        self.driver.find_element(By.ID, "su").click()
        time.sleep(2)
        self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_get_login_cookies(self):
        self.driver.get(self.BaseUrl)
        db = shelve.open("cookies")
        cookies = self.driver.get_cookies()
        print(cookies)
        cookies =[{'domain': '.work.weixin.qq.com',\
                   'httpOnly': True, \
                   'name': 'wwrtx.refid', \
                   'path': '/',\
                   'secure': False, \
                   'value': '373744626623491'}, \
                  {'domain': 'work.weixin.qq.com', \
                   'expiry': 1595100286,\
                   'httpOnly': True,\
                   'name': 'ww_rtkey', \
                   'path': '/', \
                   'secure': False, \
                   'value': '66c4f8s'}, \
                  {'domain': '.work.weixin.qq.com', \
                   'httpOnly': True, \
                   'name': 'wwrtx.ref', \
                   'path': '/', \
                   'secure': False, \
                   'value': 'direct'},\
                  {'domain': '.work.weixin.qq.com',\
                   'expiry': 1595100286,\
                   'httpOnly': False, \
                   'name': 'wwrtx.c_gdpr', \
                   'path': '/', \
                   'secure': False, \
                   'value': '0'}, \
                  {'domain': '.work.weixin.qq.com',\
                   'httpOnly': False, \
                   'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', \
                   'path': '/', \
                   'secure': False,\
                   'value': '1595068751'}, \
                  {'domain': '.work.weixin.qq.com', \
                   'expiry': 1626604751, \
                   'httpOnly': False, \
                   'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', \
                   'path': '/',\
                   'secure': False, \
                   'value': '1595068751'},\
                  {'domain': '.work.weixin.qq.com', \
                   'expiry': 1597660751, \
                   'httpOnly': False,\
                   'name': 'wwrtx.i18n_lan',\
                   'path': '/', \
                   'secure': False, \
                   'value': 'zh-cn'}]
        db["cookies"] = cookies
        cookies = db["cookies"]
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")

课后作业


import time
import shelve
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

class Test():
    def setup_method(self, method):
        options = Options()
        #和浏览器打开的调试端口进行通信
        #浏览器要使用 --remote-debugging-port=8935开启调试
        options.debugger_address = "127.0.0.1:8935"
        self.driver = webdriver.Chrome()

    def test_(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.find_element_by_id("kw").send_keys("霍格沃兹测试学院")
        self.driver.find_element_by_id("su").click()
        time.sleep(2)
        self.driver.find_element_by_link_text("霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_wework(self):
        self.driver.get("https://work.weixin.qq.com/")

        # 创建或者打开一个数据库
        db = shelve.open("cookies")
        # 获取 cookies
        #print(self.driver.get_cookies())
        #cookies=[{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'X-tTa38EQ6TwTja_kvDMhAVCg4q7fObzXhwQvBZu0ZKCU_xslYGkSqN9luzkizAHe5J1NgQRWp15dJRjmDncLyU8tUqbDQZK0v_7yy5fEoPso9v2Y0_6j0UBp3F1QWu-Rghd2e7BtErlBJhIGneg7jUIkRA7XOVKfhKPju0cFJO17Bzga_cEDT0xha_B9hd-LjMEx_f2XnYX5xBZNgOJ826dVi34yq52WjY-zQ4HRkYelUkprkmFP-0ZaT6N3OkKX1isLF1X1iRUeYR17Z0baA'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688853244183016'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970324998148082'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'UXUscewc6Zl-U6HLNpDzdgIDJoqLrYJRBQdJN71VXFcVCzoq2ISZsVO-6FYSkf2t'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a3546642'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595105852, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.qq.com', 'expiry': 1658146477, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1392988925.1594901350'}, {'domain': '.qq.com', 'expiry': 2147483645, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': 'd446d074a451879a1001b4651f3193c59069c59f5bd2cf74f8ccbe526263cbfe'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '5754147310'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626610324, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1594901346,1595074324'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.qq.com', 'expiry': 2147483646, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': '3g6lDYuDR9'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597666479, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595074324'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '220922799715615'}, {'domain': '.qq.com', 'expiry': 1595160877, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.297303679.1595074326'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595105852, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '1aph4oo'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvi', 'path': '/', 'secure': False, 'value': '5776842752'}]

        # 将数据存储到 shelve 中
        #db["cookies"] = cookies
        # 取出数据
        cookies = db["cookies"]


        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
             # 把字典加入到 driver 的 cookie中
            self.driver.add_cookie(cookie)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        #点击通讯录页面
        self.driver.find_element(By.XPATH, "//*[@id='menu_contacts']").click()
        db.close()
# -*- coding: utf-8 -*-
import shelve
from time import sleep
from selenium import webdriver
from selenium.webdriver.chrome.options import Options


class Testwork():

    def setup(self):
        # option = Options()
        # option.debugger_address = '127.0.0.1:9222'
        self.driver = webdriver.Chrome()

        self.driver.maximize_window()
        self.driver.implicitly_wait(5)

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

    def test_hogwarts(self):
        self.driver.get('http://www.baidu.com')
        self.driver.find_element_by_id('kw').send_keys('霍格沃兹测试学院')
        sleep(2)
        self.driver.find_element_by_id('su').click()
        sleep(2)
        self.driver.find_element_by_link_text('霍格沃兹测试学院 – 测试开发工程师的黄埔军校').click()
        sleep(2)

    def test_wework(self):
        # 要打开登陆后的url,并进行刷新;企业微信首页获取cookie后刷新是不会跳转到登陆后的页面的
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
        # cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'Cf32flUZRaMLgBu-mXos-n7IkWCYoWnSBUY2L-Q086uvHE0GAGR-HyiDybklmzRbI1zeByz5wxXvNEXLoz1qZlLOvrMADWC1JsI7N2FcuvIXY0dKln3-UKDmO4JKNX-KAd6D5qLrre8FMB220XBmz-gIGXDQh593kq9Lc0rSWnasRFJuyJ2IUe1t6JmUujitzJsEjnPE0f_z4Bq3GRqcrhxktvYI-cYY1RJf8DOQ7tqMnT7Nqo53GpElQM_87V-26-kWHjnuhWS2198WwJkKAQ'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': '6meavJV3VBoW_jYIyrMKSZhlYlVlviy4h2b9IVPJcnAbCm1Ko9dVexydb9C24C32'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a8748411'}, {'domain': '.qq.com', 'expiry': 1595084641, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688850372151987'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688850372151987'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325014146074'}, {'domain': '.work.weixin.qq.com', 'expiry': 1626620050, 'httpOnly': False, 'name': 'Hm_lvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595082764,1595084050'}, {'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': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'Hm_lpvt_9364e629af24cb52acc78b43e8c9f77d', 'path': '/', 'secure': False, 'value': '1595084050'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '8785491093243263'}, {'domain': '.qq.com', 'expiry': 1595170995, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.369722591.1595082741'}, {'domain': 'work.weixin.qq.com', 'expiry': 1595114272, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '1idevk'}, {'domain': '.qq.com', 'expiry': 1658156595, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.11530701.1595082741'}, {'domain': '.work.weixin.qq.com', 'expiry': 1595114272, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597676598, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}]
        # print(self.driver.get_cookies())
        # 打开数据库
        db = shelve.open("cookies")
        # 将cookies存入数据库,生成文件
        # db["cookies"] = cookies
        # 取出cookies
        cookies = db["cookies"]
        for cookie in cookies:
            # 经测试可以不需要这段代码
            # if "expiry" in cookie.keys():
            #     cookie.pop("expiry")
            self.driver.add_cookie(cookie)
        self.driver.refresh()
        # self.driver.get('https://work.weixin.qq.com/wework_admin/frame#index')
        sleep(2)
        self.driver.find_element_by_id('menu_contacts').click()
        sleep(2)
        self.driver.execute_script('window.alert("查看通讯录")')
        sleep(2)

课后作业-web

import shelve
import time

from selenium import webdriver
import pytest
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By


class TestWechat():
    def setup(self):
        options = Options()
        #添加 debugger地址
        options._debugger_address = '127.0.0.1:7891'
        # self.d = webdriver.Chrome(options=options)
        self.d = webdriver.Chrome()

    # def teardown(self):
    #     self.d.close()

    @pytest.mark.skip()
    def test_baidu(self):
        d = self.d
        d.get("https://www.baidu.com")
        d.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
        d.find_element(By.ID, "su").click()
        time.sleep(2)
        d.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_wechat(self):
        d = self.d
        d.get('https://work.weixin.qq.com/')

        #获取网页的cookies,通过调试模式,登录后调用get_cookies可以取得
        # print(d.get_cookies())
        # d.find_element(By.ID,"menu_contacts").click()


        # 可以使用shelve保存cookie保密性比较好
        # 创建或者打开一个数据库
        db = shelve.open('mycookies')
        # 添加 cookies到数据库
        # db['mycookies'] = cookies
        # 从保存的数据中读取cookies
        cookies = db['mycookies']

        #向当前页注入cookies
        for cookie in cookies:
            # expiry字段表示cookies的生效时间,登录需要移除,确保登录不被时间限制,会出现登录状态过期的错误
            if "expiry" in cookie.keys():
                cookie.pop('expiry')
            #开始注入
            d.add_cookie(cookie)
        d.get('https://work.weixin.qq.com/wework_admin/frame')
        d.find_element(By.ID, "menu_contacts").click()




class Test():

    def setup(self):
        chrome_options = Options()
        # 和浏览器打开的调试端口进行通信
        # chrome --remote-debugging-port=9222 开启调试
        chrome_options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()

    def test_baidu(self):
        self.driver.get("https://www.baidu.com/")
        self.driver.find_element(By.ID, "kw").send_keys("霍格沃兹测试学院")
        self.driver.find_element(By.ID, "su").click()
        time.sleep(3)
        self.driver.find_element(By.LINK_TEXT, "霍格沃兹测试学院 – 测试开发工程师的黄埔军校").click()

    def test_1(self):
        self.driver.find_element(By.ID, "menu_contacts").click()
        print(self.driver.get_cookies())

    def test_wework(self):

        self.driver.get("https://work.weixin.qq.com/")
        db = shelve.open("cookies")
        #将数据存储到数据库
        # db["cookies"] = cookies
        # db.close()
        #读取数据库 cookies
        cookies = db["cookies"]
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            # 把字典添加cookie中
            self.driver.add_cookie(cookie)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        time.sleep(5)
        self.driver.find_element(By.ID, "menu_contacts").click()
        db.close()

class TestWework():
    def setup_method(self, method):
        chrome_options = Options()
        #和浏览器打开的调试端口进行通信
        #浏览器要使用 --remote-debugging-port=9222 开启调试
        chrome_options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()
        #options=chrome_options


    def test_wework(self):
        self.driver.get("https://work.weixin.qq.com/")
        #self.driver.get("https://work.weixin.qq.com/wework_admin/frame#contacts")
        cookies = [{'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688850191880425'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': 'qQasHBRztctM1n0_6O76VeqP05IrviOlivP1Kv23pdgS8Wzz4bCueZU983hJ1ajCaBnOprDSxe-PJshi5TZ21o2YGOWFc_ilPaWnlWTJKfXXR3c88n9GZ5lyQDZxOdNaXgC6R3Fz945vEYs8QyTxXcwUpbwnSrLEicGix3LfrLLi7y74XU8YcCWio9JhucEbcvx8DWyyP7ZCDNSgUVeUfD8tYmCd-1ilqSLJfM9lt6gJU96l3wkvE4mcG1DkwPAuGfSFM1gQ8yeYe9xxlh8GKA'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688850191880425'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970324985149794'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a8595229'}, {'domain': '.qq.com', 'expiry': 1595041315, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.1588439291.1594954904'}, {'domain': 'work.weixin.qq.com', 'expiry': 1594986439, 'httpOnly': True, 'name': 'ww_rtkey', 'path': '/', 'secure': False, 'value': '96t61ug'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '576210297103790'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': '8qaX0xRZvBzO9YiRlB_V2HYTr6suVYS6RBwbIwiH9aXQZ3WqCYXmy7ScIf-kECh4'}, {'domain': '.qq.com', 'expiry': 1594954963, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.qq.com', 'expiry': 1658026915, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.925237569.1594954904'}, {'domain': '.work.weixin.qq.com', 'expiry': 1594986439, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}, {'domain': '.work.weixin.qq.com', 'expiry': 1597546924, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh-cn'}]
        #获取 cookies
        print(self.driver.get_cookies())
        for cookie in cookies:
            if "expiry" in cookie.keys():
                cookie.pop("expiry")
            # 把字典加入到 driver 的 cookie 中
            self.driver.add_cookie(cookie)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.find_element(By.XPATH, "//*[@id='menu_contacts']").click()

关闭