Selenium(Web自动化工具)

1. 先下载相应的浏览器driver, 并配置环境变量

2. 下载python selenium包  pip install selenium

pycharm 中ctrl+alt+l   对文档内容进行格式化

直接等待  time.sleep(3)

隐式等待  self.driver.implicitly_wait(3)  动态的等待

显示等待  WebDriverWait  配合until()

find_element(By.ID, ‘#kw‘).send_keys(‘霍格沃兹‘)

find_element(By.ID, ‘#su‘).click()

xpath

css selector

Base封装

from selenium import webdriverclass Base:def setup(self):self.driver = webdriver.Firefox()self.driver.implicitly_wait(5)self.driver.maximize_window()def teardown(self):self.driver.quit()

百度浏览器测试

from selenium import webdriverclass TestHogWords:def setup(self):self.driver = webdriver.Firefox()   # 获取浏览器        self.driver.maximize_window()       # 窗口最大化        self.driver.implicitly_wait(20)    # 隐式动态等待时间    def teardown(self):self.driver.quit()      # driver回收    def test_demo(self):self.driver.get("https://www.baidu.com")self.driver.find_element_by_link_text("新闻").click()self.driver.find_element_by_link_text("国内").click()

窗口切换

from time import sleepfrom selenium.webdriver.common.by import Byfrom selenium_learning.base import Baseclass TestWindows(Base):def test_windows(self):self.driver.get("https://www.baidu.com")self.driver.find_element(By.CSS_SELECTOR, ‘.s-top-login-btn‘).click()self.driver.find_element(By.LINK_TEXT, "立即注册").click()        sleep(3)   # 需要一定的延迟        windows = self.driver.window_handlesprint(windows)# print(self.driver.current_window_handle)        # print(windows)        self.driver.switch_to.window(windows[-1])print(self.driver.current_window_handle)self.driver.find_element(By.CSS_SELECTOR, ‘#TANGRAM__PSP_4__userName‘).send_keys(‘alex‘)self.driver.find_element(By.CSS_SELECTOR, ‘#TANGRAM__PSP_4__phone‘).send_keys(‘15250280726‘)self.driver.switch_to.window(windows[0])self.driver.find_element(By.CSS_SELECTOR, ‘#TANGRAM__PSP_10__footerULoginBtn‘).click()        sleep(3)

左击, 右击,双击,拖拽操作

from time import sleepimport pytestfrom selenium import webdriverfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.by import Byclass TestActionChains:def setup(self):self.driver = webdriver.Firefox()def teardown(self):self.driver.quit()@pytest.mark.skip    def test_action(self):self.driver.get("http://sahitest.com/demo/clicks.htm")        element_click = self.driver.find_element(By.XPATH, ‘//input[@value="click me"]‘)        element_right_click = self.driver.find_element(By.XPATH, ‘//input[@value="right click me"]‘)        element_double_click = self.driver.find_element(By.XPATH, ‘//input[@value="dbl click me"]‘)        action = ActionChains(self.driver)        action.click(element_click)        action.context_click(element_right_click)        action.double_click(element_double_click)        action.perform()        sleep(3)@pytest.mark.skip    def test_move_to_element(self):self.driver.get("https://www.baidu.com")        element_one = self.driver.find_element(By.CSS_SELECTOR, ‘#s-usersetting-top‘)        action = ActionChains(self.driver)        action.move_to_element(element_one)        action.perform()        sleep(3)def test_dragdrop(self):self.driver.get("http://sahitest.com/demo//dragDropMooTools.htm")        drag_element = self.driver.find_element(By.CSS_SELECTOR, ‘#dragger‘)        drop_element = self.driver.find_element(By.CSS_SELECTOR, ‘div.item:nth-child(4)‘)        action = ActionChains(self.driver)        action.drag_and_drop(drag_element,drop_element)#action.click_and_hold(drag_element).release(drop_element).perform()        action.perform()        sleep(3)frame切换和alert弹窗处理from time import sleepfrom selenium.webdriver import ActionChainsfrom selenium.webdriver.common.by import Byfrom selenium_learning.base import Baseclass TestAlert(Base):def test_alert(self):self.driver.get("https://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")self.driver.switch_to_frame("iframeResult")   #切换至iframeReslut窗口        drag = self.driver.find_element(By.CSS_SELECTOR, ‘#draggable‘)        drop = self.driver.find_element(By.CSS_SELECTOR, ‘#droppable‘)        action = ActionChains(self.driver)        action.drag_and_drop(drag, drop).perform()        sleep(3)self.driver.switch_to_alert().accept()        sleep(3)self.driver.switch_to_default_content()   #回归默认窗口        sleep(3)页面滑动操作
from time import sleepfrom selenium import webdriverfrom selenium.webdriver import TouchActionsfrom selenium.webdriver.common.by import Byclass TestTouchAction:def setup(self):        option = webdriver.ChromeOptions()        option.add_experimental_option(‘w3c‘, False)self.driver = webdriver.Chrome(options=option)self.driver.implicitly_wait(5)def teardown(self):self.driver.quit()def test_demo(self):self.driver.get("https://www.baidu.com")        element_input = self.driver.find_element(By.CSS_SELECTOR, ‘#kw‘)        element_search = self.driver.find_element(By.CSS_SELECTOR, ‘#su‘)        element_input.send_keys(‘selenium测试‘)        action = TouchActions(self.driver)        action.tap(element_search)   # 点击        action.perform()        action.scroll_from_element(element_input, 0, 10000).perform()   # 滑动操作        sleep(3)
from time import sleepimport pytestfrom selenium.webdriver.common.by import Byfrom selenium_learning.base import Baseclass TestDemo(Base):@pytest.mark.skip    def test_demo(self):self.driver.get("https://www.baidu.com")self.driver.find_element(By.CSS_SELECTOR, ‘#kw‘).send_keys("selenium测试")self.driver.find_element(By.CSS_SELECTOR, ‘#su‘).click()self.driver.execute_script("document.documentElement.scrollTop=10000")        sleep(5)self.driver.find_element(By.CSS_SELECTOR, ‘.n‘).click()        sleep(2)def test_12306(self):self.driver.get("https://www.12306.cn/index/")self.driver.execute_script("document.getElementById(‘train_date‘).removeAttribute(‘readonly‘)")        sleep(3)self.driver.execute_script("document.getElementById(‘train_date‘).value=‘2020-5-20‘")        sleep(5)利用cookies进行企业微信web自动化登录import shelvefrom time import sleepfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Optionsfrom selenium.webdriver.common.by import Byclass TestCookie:def setup(self):        options = Options()        options.debugger_address = "127.0.0.1:8081"        # self.driver = webdriver.Firefox(options=options)        self.driver = webdriver.Firefox()self.driver.implicitly_wait(5)self.driver.maximize_window()def teardown(self):self.driver.quit()def test_cookie(self):self.driver.get("https://work.weixin.qq.com/wework_admin/frame")        db = shelve.open("test_db")# db[‘cookie‘] = self.driver.get_cookies()   #获取网页cookies(),保存在test_db中        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‘)self.driver.find_element(By.CSS_SELECTOR, ‘#menu_contacts > span‘).click()        sleep(3)        db.close()

相关推荐