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()
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()
dcbh
(淡茶白花)
2020 年4 月 4 日 08:48
15
nancy
(杨亚楠)
2020 年4 月 12 日 09:05
20
使用复用浏览器技术获取企业微信的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("技术总监")
timothy
(提摩太)
2020 年7 月 30 日 05:55
29
Byy
2020 年8 月 19 日 15:09
31
使用复用浏览器技术获取企业微信的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()