RUN__IT # selenium定位截图

Selenium网页截图技巧
本文介绍使用Selenium进行网页截图的方法,包括全网页截图及定位特定元素进行截图的详细步骤。通过Python代码示例,展示了如何利用Selenium WebDriver和PIL库实现网页截图功能。

selenium定位截图


from selenium import webdriver
import time
from PIL import Image
 
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
time.sleep(3)
 
# 演示一:全网页截图
# driver.save_screenshot('screenshot.png')
# driver.quit()
 
# 演示二:定位区块截图
driver.save_screenshot(r'photo.png')  # 一次截图:形成全图
baidu = driver.find_element_by_id('su')  # 截图按钮百度一下
# baidu = driver.find_element_by_xpath("//div[@id='lg']/img[@class='index-logo-src']") #截图百度logo图片
# print(baidu)
left = baidu.location['x']  # 区块截图左上角在网页中的x坐标
top = baidu.location['y']  # 区块截图左上角在网页中的y坐标
right = left + baidu.size['width']  # 区块截图右下角在网页中的x坐标
bottom = top + baidu.size['height']  # 区块截图右下角在网页中的y坐标
# print({"left": left, "top": top, "right": right, "bottom ": bottom})
# print("baidu.size['width']:%s" % baidu.size['width'])
# print("baidu.size['height']:%s" % baidu.size['height'])
picture = Image.open(r'photo.png')
picture = picture.crop((left, top, right, bottom))  # 二次截图:形成区块截图
picture.save(r'photo2.png')
driver.quit()
import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.keys import Keys from webdriver_manager.microsoft import EdgeChromiumDriverManager class TestChaoxing: def setup_method(self, method): # 使用WebDriverManager自动管理Edge驱动 self.driver = webdriver.Edge(EdgeChromiumDriverManager().install()) self.driver.maximize_window() self.wait = WebDriverWait(self.driver, 60) # 增加等待时间到60秒 self.actions = ActionChains(self.driver) self.vars = {} def teardown_method(self, method): if self.driver: self.driver.quit() def wait_for_window(self, timeout): time.sleep(timeout) wh_now = self.driver.window_handles wh_then = self.vars.get("window_handles") if wh_then and len(wh_now) > len(wh_then): return list(set(wh_now) - set(wh_then))[0] return wh_now[-1] def test_chaoxing(self): try: # 1. 打开登录页面 self.driver.get("https://passport2.chaoxing.com/login?fid=&newversion=true&refer=https%3A%2F%2Fi.chaoxing.com") # 2. 登录操作 self.wait.until(EC.visibility_of_element_located((By.ID, "phone"))).send_keys("17651727533") self.driver.find_element(By.ID, "pwd").send_keys("z123456789") self.driver.find_element(By.CSS_SELECTOR, ".check-input").click() self.driver.find_element(By.ID, "loginBtn").click() # 3. 等待登录完成并切换到iframe self.wait.until(EC.frame_to_be_available_and_switch_to_it(0)) # 4. 滚动页面 self.driver.execute_script("window.scrollTo(0,400)") # 5. 点击图片打开新窗口 self.vars["window_handles"] = self.driver.window_handles self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#c_249884772 img"))).click() # 6. 切换到新窗口 self.vars["win6528"] = self.wait_for_window(2) self.driver.switch_to.window(self.vars["win6528"]) # 7. 点击导航链接 self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#nav_16068 > a"))).click() # 8. 切换到iframe并填写标题 self.wait.until(EC.frame_to_be_available_and_switch_to_it(0)) title_input = self.wait.until(EC.element_to_be_clickable((By.NAME, "title"))) title_input.click() title_input.send_keys("测试") # 9. 切换到内容编辑iframe self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "ueditor_0"))) content_editable = self.wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".view:nth-child(2)"))) # 点击并设置内容 content_editable.click() self.driver.execute_script("arguments[0].innerHTML = &#39;<p element-id=\"init\">20230401084周志灏</p >&#39;", content_editable) # 10. 返回主文档并点击发布按钮 self.driver.switch_to.parent_frame() publish_btn = self.wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".edit_btn > .jb_btn"))) # 使用Actions模拟更自然的点击 self.actions.move_to_element(publish_btn).pause(0.5).click().pause(0.5).perform() # 11. 点击发布的话题(使用XPath内容定位) published_topic = self.wait.until(EC.element_to_be_clickable( (By.XPATH, "//*[contains(text(),&#39;20230401084周志灏&#39;)]"))) self.driver.execute_script("arguments[0].scrollIntoView(true);", published_topic) # 获取点击前所有窗口句柄 old_windows = self.driver.window_handles published_topic.click() time.sleep(2) # 等待页面打开 # 12. 获取新窗口句柄并切换 new_windows = self.driver.window_handles new_window = list(set(new_windows) - set(old_windows)) if new_window: self.driver.switch_to.window(new_window[0]) time.sleep(2) # 成功切换后观察页面 else: raise Exception("未检测到新窗口打开,可能是话题在原窗口打开") # 13.1 点赞话题 like_btn = self.wait.until(EC.element_to_be_clickable( (By.CSS_SELECTOR, ".likeIcon"))) self.driver.execute_script("arguments[0].scrollIntoView(true);", like_btn) like_btn.click() time.sleep(1) # 13.2 点击回复框(通过 XPath 定位) reply_box = self.wait.until(EC.element_to_be_clickable( (By.XPATH, "/html/body/div[1]/div[2]/div[3]/div[2]/div/div[1]/textarea"))) reply_box.click() reply_box.send_keys("OK啦") time.sleep(0.5) # 提交回复 reply_submit_btn = self.wait.until(EC.element_to_be_clickable( (By.CSS_SELECTOR, ".addReply"))) reply_submit_btn.click() time.sleep(2) # 15. 点击"删除"链接(通过 linkText 定位) delete_link = self.wait.until(EC.element_to_be_clickable( (By.LINK_TEXT, "删除"))) self.driver.execute_script("arguments[0].scrollIntoView(true);", delete_link) delete_link.click() time.sleep(1) # 16. 点击弹窗中的"确认删除"按钮 confirm_delete_btn = self.wait.until(EC.element_to_be_clickable( (By.CSS_SELECTOR, ".sureDelete"))) confirm_delete_btn.click() time.sleep(2) except Exception as e: print(f"测试过程中发生错误: {str(e)}") raise # 如果要使用pytest运行,可以添加以下内容 if __name__ == "__main__": test = TestChaoxing() test.setup_method(None) try: test.test_chaoxing() finally: test.teardown_method(None)在pycharm中运行显示进程已结束,退出代码为1,是什么原因
最新发布
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值