selenium 企业微信实战1

上课补充上课补充上课补充上课补充上课补充

selenium python官网:https://seleniumhq.github.io/selenium/docs/api/py/

selenium IDE

https://www.selenium.dev/downloads/

浏览器复用

开启浏览器:

chrome --remote-debugging-port=9222

python

# Generated by Selenium IDE
from time import sleep

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


class TestDemo():
    def setup_method(self, method):
        options = Options()
        options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome(options=options)
        self.vars = {}

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

    def test_demo(self):
        #self.driver.get("https://ceshiren.com/")
        self.driver.find_element(By.ID, 'menu_contacts').click()
        sleep(5)

cookies

# Generated by Selenium IDE
import shelve
from time import sleep

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


class TestDemo():
    def setup_method(self, method):
        options = Options()
        options.debugger_address = "127.0.0.1:9222"
        self.driver = webdriver.Chrome()
        self.vars = {}

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

    def test_demo(self):
        #print(self.driver.get_cookies())
        self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
        db = shelve.open("cookies")
        #db['cookie'] = self.driver.get_cookies()
        cookies = db['cookie']
        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')
        sleep(3)
        db.close()


关闭