随笔:python+selenium进行UI自动化循环删除一组元素2

随笔:python+selenium进行UI自动化删除一组元素2

跟第一篇有点区别,适用不同的场景

#coding = utf-8


import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains


class Deletetwo(unittest.TestCase):

    def setUp(self):
        # 屏蔽自动化受控提示 && 开发者提示
        self.option = webdriver.ChromeOptions()
        self.option.add_experimental_option("excludeSwitches", ['enable-automation', 'load-extension'])
        # 屏蔽'保存密码'提示框
        self.prefs = {}
        self.prefs["credentials_enable_service"] = False
        self.prefs["profile.password_manager_enabled"] = False
        self.option.add_experimental_option("prefs", self.prefs)
        # 打开谷歌浏览器
        self.driver = webdriver.Chrome(options=self.option)
        # 窗口最大化
        self.driver.maximize_window()
        sleep(5)
        self.driver.get(url1)
        sleep(10)
        self.driver.find_element_by_xpath("//button/span[text()='前往AI生态集市']").click()
        sleep(20)
        self.driver.get(url2)
        sleep(10)
        self.assertEqual("cloudAI自动化", self.driver.find_element_by_xpath("//div[text()='cloudAI自动化']").text)
        sleep(2)


    def testdeletetwo(self):
        ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//div[text()='cloudAI自动化']")).perform()
        sleep(1)
        ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//a/span[text() = '个人中心']")).perform()
        self.driver.find_element_by_xpath("//a/span[text() = '个人中心']").click()
        sleep(5)
        self.driver.find_element_by_xpath("//div[@id='tab-seventh']/span[contains(text(),'收藏')]").click()
        sleep(5)

        #进行循环删除
        while True:
            els = self.driver.find_elements_by_xpath("//div[@id='pane-seventh']//span[contains(text(),'收藏于')]")
            sleep(10)
            print(els)
            print(len(els))
            sleep(1)
            ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//div[@id='pane-seventh']//span[contains(text(),'收藏于')]")).perform()
            sleep(2)
            ActionChains(self.driver).move_to_element(self.driver.find_element_by_xpath("//span[contains(text(),'收藏于')]/../..//span[text()='取消收藏']")).perform()
            self.driver.find_element_by_xpath("//span[contains(text(),'收藏于')]/../..//span[text()='取消收藏']").click()
            sleep(1)
            self.driver.find_element_by_xpath("//div[@aria-label='提示']//span[contains(text(),'确定')]").click()
            sleep(2)
            if len(els)-1 == 0:
                break

        #确认是否成功完成
        deleteelstwo=self.driver.find_elements_by_xpath("//div[@id='pane-seventh']//span[contains(text(),'收藏于')]")
        self.assertTrue(len(deleteelstwo)==0)

    def tearDown(self):
        self.driver.quit()
        print("***测试结束***")


    if __name__ == '__main__':
        unittest.main()

你可能感兴趣的:(随笔)