from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # 启动 Chrome 浏览器 driver = webdriver.Chrome() try: # 步骤1:进入首页 driver.get("http://localhost:8080/#/") # 步骤2:点击登录按钮 login_button = driver.find_element_by_xpath("//span[text()='登录']") login_button.click() # 步骤3:输入账号和密码,点击登录 username_input = driver.find_element_by_css_selector("input.el-input__inner[placeholder='用户名']") password_input = driver.find_element_by_css_selector("input.el-input__inner[placeholder='密码']") login_button = driver.find_element_by_xpath("//span[text()='登录']") username_input.send_keys("Yingjie") password_input.send_keys("123456") login_button.click() # 等待登录成功 WebDriverWait(driver, 10).until(EC.url_contains("#/")) # 步骤4:点击分类按钮查看文章分类 category_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//li[@class='el-menu-item' and text()='文章分类']")) ) category_element.click() # 步骤5:查看后端分类对应的文章列表 back_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//h4[@class='me-allct-name' and text()='后端']")) ) back_element.click() # 步骤6:点击标签按钮查看标签 tag_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//li[@class='el-menu-item' and text()='标签']")) ) tag_element.click() # 步骤7:查看spring标签对应的文章列表 spring_element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.XPATH, "//h4[@class='me-allct-name' and text()='springmvc']")) ) spring_element.click() # 步骤8:查看文章详情 try: # 使用 CSS 选择器定位文章标题链接元素 article_link = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, '.me-article-header > a.me-article-title')) ) article_link.click() except Exception as e: print("发生异常:", e) # # 步骤8:查看文章归档列表 # articles_element = WebDriverWait(driver, 20).until( # EC.presence_of_element_located((By.XPATH, "//li[@class='el-menu-item is-active' and text()='文章归档']")) # ) # articles_element.click() # 步骤9:写文章 write_element = driver.find_element_by_xpath("//li[@class='el-menu-item' and contains(text(),'写文章')]") write_element.click() # 获取输入标题元素 try: # 使用显式等待确保元素加载 wait = WebDriverWait(driver, 10) # 等待最多10秒 title_textarea = wait.until( EC.presence_of_element_located( (By.XPATH, "//div[@class='me-write-input el-textarea']/textarea[@placeholder='请输入标题']")) ) # 一旦找到,执行相关操作 title_textarea.send_keys("Selenium测试3") except TimeoutException: print("超时,未找到元素") # 输入文章内容 try: # 使用显式等待确保元素加载 wait = WebDriverWait(driver, 10) # 等待最多10秒 textarea = wait.until( EC.presence_of_element_located((By.CSS_SELECTOR, "textarea[placeholder='开始编辑...']")) ) # 一旦找到,执行相关操作,输入文本 textarea.send_keys("这是我要输入的文本") except Exception as e: print("发生异常:", e) # 点击发布 try: # 定位父元素 parent_element = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CLASS_NAME, "me-write-btn")) ) # 在父元素中查找发布按钮 publish_button = parent_element.find_element(By.XPATH, "//span[contains(text(), '发布')]") # 点击发布按钮 publish_button.click() except Exception as e: print("发生异常:", e) # 摘要 try: # 使用 CSS 选择器定位文本框元素 summary_textarea = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.CSS_SELECTOR, 'textarea[placeholder="请输入摘要"]')) ) # 在文本框中输入内容 summary_textarea.send_keys("测试") except Exception as e: print("发生异常:", e) # 下拉框 try: # 等待下拉框可点击 dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".el-input__inner"))) # 点击下拉框以展开选项 dropdown.click() # 等待下拉框选项出现 dropdown_option = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='后端']"))) # 选择下拉框的值 dropdown_option.click() except Exception as e: print("发生异常:", e) # 标签 try: # 等待文章标签元素可见 tag_checkbox = WebDriverWait(driver, 10).until( EC.visibility_of_element_located((By.XPATH, "//span[text()='springboot']"))) # 点击文章标签复选框 tag_checkbox.click() except Exception as e: print("发生异常:", e) # 发布 try: # 使用 XPath 定位 "发布" 按钮元素 publish_button = WebDriverWait(driver, 10).until( EC.element_to_be_clickable( (By.XPATH, '//button[contains(@class, "el-button--primary") and .//span[contains(text(), "发布")]]')) ) # 点击发布按钮 publish_button.click() except Exception as e: print("发生异常:", e) # 步骤10:评论 try: # 使用 XPath 定位评论文本框元素 comment_textarea = WebDriverWait(driver, 10).until( EC.presence_of_element_located( (By.XPATH, '//div[contains(@class, "me-view-comment-text")]//textarea[@placeholder="你的评论..."]')) ) # 在评论文本框中输入内容 comment_textarea.send_keys("这是我的评论。") except Exception as e: print("发生异常:", e) # 提交评论 try: # 使用 CSS 选择器定位评论按钮元素 comment_button = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.CSS_SELECTOR, 'button.el-button.el-button--text span')) ) # 使用 JavaScript 执行点击操作 driver.execute_script("arguments[0].click();", comment_button) except Exception as e: print("发生异常:", e) finally: # 关闭浏览器 driver.quit()
标签:10,Selenium,driver,博客,element,EC,自动化,until,WebDriverWait From: https://www.cnblogs.com/zyj3955/p/18176535