打印屏蔽部分内容window.print()

这段代码主要用于网页打印预处理,它寻找特定的打印标识符并截取它们之间的内容作为打印区域,然后调用浏览器的打印功能。在打印完成后,原始页面内容会被恢复。
	var bdhtml=window.document.body.innerHTML;
	var sprnstr="<!--startprint-->"; //开始打印标识字符串有17个字符
	var eprnstr="<!--endprint-->"; //结束打印标识字符串
	var prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17); //从开始打印标识之后的内容
	prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //截取开始标识和结束标识之间的内容
	window.document.body.innerHTML=prnhtml; //把需要打印的指定内容赋给body.innerHTML
	window.print(); //调用浏览器的打印功能打印指定区域
	window.document.body.innerHTML=bdhtml;//重新给页面内容赋值;
问题1.会打开2个窗口,一个是正常的,一个是data:, from django.contrib.auth.decorators import login_required from selenium import webdriver from selenium.webdriver.chrome.options import Options as ChromeOptions from selenium.webdriver.edge.options import Options as EdgeOptions from selenium.webdriver.firefox.options import Options as FirefoxOptions from selenium.webdriver.chrome.service import Service as ChromeService from selenium.webdriver.edge.service import Service as EdgeService from selenium.webdriver.firefox.service import Service as FirefoxService from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.action_chains import ActionChains from selenium.common.exceptions import TimeoutException, ElementClickInterceptedException import time #浏览器配置 class BrowserConfig: def __init__(self, browser_type='edge', headless=False): self.browser_type = browser_type.lower() self.headless = headless self.options = self._setup_options() self.driver_path = self._get_driver_path() def _setup_options(self): options = { 'chrome': ChromeOptions(), 'edge': EdgeOptions(), 'firefox': FirefoxOptions() } if self.browser_type not in options: raise ValueError(f"Unsupported browser: {self.browser_type}") if self.headless: if self.browser_type == 'chrome': options[self.browser_type].add_argument('--headless=new') options[self.browser_type].add_argument('--disable-gpu') elif self.browser_type == 'edge': options[self.browser_type].add_argument('--headless=chrome') elif self.browser_type == 'firefox': options[self.browser_type].add_argument('--headless') return options[self.browser_type] def _get_driver_path(self): driver_paths = { 'edge': r"C:\own\app\python_code\work\入职培训\打印点击\msedgedriver.exe" } return driver_paths.get(self.browser_type) def get_driver(self): if self.browser_type == 'chrome': return webdriver.Chrome(options=self.options) elif self.browser_type == 'edge': if not self.driver_path: raise ValueError("Edge driver path is required.") service = EdgeService(executable_path=self.driver_path) return webdriver.Edge(service=service, options=self.options) elif self.browser_type == 'firefox': return webdriver.Firefox(options=self.options) else: raise ValueError(f"Unsupported browser: {self.browser_type}") #页面元素处理 class ElementActions: def __init__(self, driver): self.driver = driver self.action = ActionChains(driver) def scroll_to_element(self, element): try: self.driver.execute_script("arguments[0].scrollIntoView({block: 'center'});", element) time.sleep(0.3) except Exception as e: print(f"滚动失败: {e}") def safe_click(self, element): try: element.click() except ElementClickInterceptedException: print("元素被遮挡,尝试使用 JavaScript 点击") self.driver.execute_script("arguments[0].click();", element) def wait_for_element(self, by, locator, timeout=10): return WebDriverWait(self.driver, timeout).until( EC.presence_of_element_located((by, locator))) #获取加载后不在父元素下的元素 def get_page_state(self): return { "url": self.driver.current_url, "title": self.driver.title, "window_handles": len(self.driver.window_handles), "body_text": self.driver.find_element(By.TAG_NAME, "body").text, "page_height": self.driver.execute_script("return document.body.scrollHeight;"), "html_hash": hash(self.driver.find_element(By.TAG_NAME, "html").get_attribute("outerHTML")) } def compare_states(self, before, after): changes = {} for key in before: if before[key] != after[key]: changes[key] = {"before": before[key], "after": after[key]} return changes def get_all_button_elements(self): """获取当前页面中所有可见的 button 元素""" script = """ const buttons = Array.from(document.querySelectorAll('button')); return buttons.filter(btn => { const style = window.getComputedStyle(btn); return style.display !== 'none' && style.visibility !== 'hidden'; }); """ return self.driver.execute_script(script) def click_and_detect_changes(self, element): # 记录点击前状态 before_state = self.get_page_state() before_buttons = self.get_all_button_elements() # print("点击前页面状态:", before_state) try: element.click() except Exception as e: print("点击失败:", e) return [] try:#点击后等待按钮加载 WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, 'button')) ) except TimeoutException: print("等待按钮加载超时") time.sleep(1) after_state = self.get_page_state() after_buttons = self.get_all_button_elements() # print("点击后页面状态:", after_state) changes = self.compare_states(before_state, after_state) if changes: print("检测到页面变化:") # 判断按钮是否是默认展开的 if len(after_buttons) < len(before_buttons): try: before_buttons_s = self.get_all_button_elements() # 列表收起时的按钮 element.click() except Exception as e: print("点击失败:", e) return [] after_buttons_1 = self.get_all_button_elements() new_buttons = list(set(after_buttons_1) - set(before_buttons_s)) try: WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.TAG_NAME, 'button')) ) except TimeoutException: print("等待按钮加载超时") time.sleep(1) else: new_buttons = list(set(after_buttons) - set(before_buttons)) else: print("页面无变化") return new_buttons class Settings_handler(): def __init__(self, browser_config): self.browser_config = browser_config self.driver = browser_config.get_driver() self.element_actions = ElementActions(self.driver) print(f"Browser {self.browser_config.browser_type} started in " f"{'headless' if self.browser_config.headless else 'headed'} mode.") def click_direct_settings_button(self): try: settings_button=self.element_actions.wait_for_element(By.XPATH, "//button[contains(text(), 'Settings')]") self.element_actions.safe_click(settings_button) # 点击设置下的按钮 new_setting_button = self.element_actions.click_and_detect_changes(settings_button) if new_setting_button: print(f"设置菜单下找到 {len(new_setting_button)} 个新增按钮:", new_setting_button) for btn in new_setting_button: try: self.element_actions.safe_click(btn) print(f"已点击按钮: {btn.text or '未知按钮'}") time.sleep(0.5) except Exception as e: print(f"点击按钮时出错: {str(e)}") else: print("未检测到新增按钮") return True except TimeoutException: print("未找到直接可见的设置按钮") return False #设置在下拉菜单中 #找到设置 def click_settings_in_dropdowns(self): # 所有可能展开设置的父元素 parent_elements = self.driver.find_elements(By.XPATH, "//button[@aria-haspopup='true'] | //div[@role='menu']") for parent in parent_elements: if not parent.is_displayed() or not parent.is_enabled(): continue print("尝试点击展开元素") new_buttons = self.element_actions.click_and_detect_changes(parent) for btn in new_buttons: try: if "setting" in btn.text.lower() or "设置" in btn.text.lower(): print(f"发现设置按钮: {btn.text}") self.element_actions.safe_click(btn) #点击设置下的按钮 new_setting_button=self.element_actions.click_and_detect_changes(btn) if new_setting_button: print(f"设置菜单下找到 {len(new_setting_button)} 个新增按钮:", new_setting_button) for btn in new_setting_button: try: self.element_actions.safe_click(btn) print(f"已点击按钮: {btn.text or '未知按钮'}") time.sleep(0.5) except Exception as e: print(f"点击按钮时出错: {str(e)}") else: print("未检测到新增按钮") return True except Exception: continue print("未在下拉菜单中找到设置按钮") return False def click_settings_button(self): #点击设置并点击设置下的可点击的设置 if self.click_direct_settings_button(): return True # 如果没找到,尝试点击下拉菜单查找设置按钮并点击可点击的设置 if self.click_settings_in_dropdowns(): return True print("未找到设置按钮") return False #页面操作实现 class AutomationWorkflow: def __init__(self, browser_config, login_url="http://10.244.1.179:3000/"): self.browser_config = browser_config self.login_url = login_url self.driver = browser_config.get_driver() self.element_actions = ElementActions(self.driver) self.driver.get(self.login_url) self.set_cl=Settings_handler(self.browser_config) print(f"Browser {self.browser_config.browser_type} started in " f"{'headless' if self.browser_config.headless else 'headed'} mode.") def login(self, email, password): try: email_input = self.element_actions.wait_for_element(By.NAME, 'email') email_input.send_keys(email) password_input = self.element_actions.wait_for_element(By.NAME, 'current-password') password_input.send_keys(password) login_button = self.driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]') login_button.click() time.sleep(1) return True except TimeoutException: print("登录失败,元素未加载或超时") return False except Exception as e: print(f"登录过程中发生错误: {str(e)}") return False def logout(self): try: user_button = self.element_actions.wait_for_element( By.XPATH, "//button[@aria-label='User Menu']") self.element_actions.safe_click(user_button) logout_button = self.driver.find_element( By.XPATH, "//div[text()='サインアウト']") self.element_actions.safe_click(logout_button) print("登出成功") except Exception as e: print(f"登出过程中发生错误: {str(e)}") def click_all_clickable_elements(self): try: elements_locator = (By.CSS_SELECTOR, 'button, a, input[type="button"], input[type="submit"], ' 'div[role="button"], div[onclick], [role="menuitem"], ' '[role="option"], [data-testid*="button"], [class*="btn"], ' '[class*="clickable"]') elements = WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located(elements_locator)) count = len(elements) print(f"初始找到 {count} 个可能可点击的元素") original_window = self.driver.current_window_handle original_url = self.driver.current_url for index in range(count): try: elements = self.driver.find_elements(*elements_locator) if index >= len(elements): print(f"元素列表已变化,跳过索引 {index}") continue element = elements[index] self.element_actions.scroll_to_element(element) if element.is_displayed() and element.is_enabled(): element.click() print(f"已点击元素 {index + 1}/{count}") time.sleep(0.5) try: alert = WebDriverWait(self.driver, 3).until( EC.alert_is_present()) print("检测到弹窗,正在处理...") alert.accept() except TimeoutException: print("无弹窗") time.sleep(0.2) current_windows = self.driver.window_handles if len(current_windows) > 1: for window in current_windows: if window != original_window: self.driver.switch_to.window(window) self.driver.close() self.driver.switch_to.window(original_window) else: if self.driver.current_url != original_url: self.driver.back() WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located(elements_locator)) except Exception as e: print(f"点击元素 {index + 1} 时出错: {str(e)}") continue except Exception as e: print(f"处理可点击元素时出错: {str(e)}") def handle_input_fields(self, test_value="12"): try: input_elements = self.driver.find_elements(By.CSS_SELECTOR, 'input[type="text"], input[type="email"], input[type="number"], ' 'input[type="password"], input[type="tel"], input[type="url"], ' 'textarea, [role="textbox"], [contenteditable="true"]') print(f"找到 {len(input_elements)} 个输入框") original_window = self.driver.current_window_handle original_url = self.driver.current_url for index, element in enumerate(input_elements): try: self.element_actions.scroll_to_element(element) element.clear() element.send_keys(test_value) print(f"已在输入框 {index + 1} 中输入测试值") form = element.find_element(By.XPATH, "./ancestor::form[1]") if form: submit_buttons = form.find_elements(By.CSS_SELECTOR, 'button[type="submit"], input[type="submit"]') if submit_buttons: submit_buttons[0].click() print(f"已提交输入框 {index + 1} 所在的表单") time.sleep(1) current_windows = self.driver.window_handles if len(current_windows) > 1: for window in current_windows: if window != original_window: self.driver.switch_to.window(window) self.driver.close() self.driver.switch_to.window(original_window) else: if self.driver.current_url != original_url: self.driver.back() time.sleep(2) except Exception as e: print(f"处理输入框 {index + 1} 时出错: {str(e)}") except Exception as e: print(f"处理输入框时出错: {str(e)}") def run_automation(self, email, password,login_required=True): if login_required and self.login_url: if not self.login(email, password): print("登陆失败") return print("开始自动化操作...") # self.click_all_clickable_elements() self.handle_input_fields() time.sleep(2) self.set_cl.click_settings_button() self.logout() print("自动化操作完成") time.sleep(3) self.driver.quit() if __name__ == "__main__": login_url = "http://10.244.1.179:3000/" email = "shengkai.qu@brother-bsh.com.cn" password = "q123456.+" login_required=True browser_config = BrowserConfig(browser_type='edge', headless=False) automation_workflow = AutomationWorkflow( browser_config=browser_config, login_url=login_url) automation_workflow.run_automation(email, password,login_required)
最新发布
08-19
### PDF.js 禁用打印和下载功能实现方式 在使用 `PDF.js` 进行 PDF 文件预览时,可以通过修改其默认行为来禁用打印和下载功能。以下是几种常见的方法: #### 方法一:隐藏按钮 通过 CSS 隐藏页面中的打印和下载按钮是一种简单有效的方式。可以在加载 `viewer.html` 的时候,针对这些按钮设置样式属性。 ```css /* 使用CSS隐藏打印和下载按钮 */ #download { display: none !important; } #print { display: none !important; } ``` 这种方法适用于基于 HTML 页面嵌入的场景[^3],能够快速移除界面中的交互选项。 --- #### 方法二:重写函数逻辑 如果需要更深层次地控制打印和下载的行为,则可以覆盖 `PDF.js` 中的相关事件处理程序或函数定义。例如,在初始化阶段重新绑定打印和下载操作为空函数。 ```javascript // 覆盖打印功能 window.print = function() {}; // 移除下载链接的功能 document.querySelectorAll('.download').forEach(function(el) { el.addEventListener('click', function(event) { event.preventDefault(); event.stopPropagation(); }); }); ``` 此脚本片段会阻止用户触发任何与打印有关的动作以及点击下载链接的操作[^1]。 --- #### 方法三:自定义构建 PDFViewerApplication 对于更加复杂的项目需求,比如 Vue CLI 或 React 应用集成,推荐创建一个定制化的 PDF 查看器实例,并显式关闭不需要的服务接口。 ```javascript import { getDocument, GlobalWorkerOptions } from 'pdfjs-dist'; GlobalWorkerOptions.workerSrc = '//mozilla.github.io/pdf.js/build/pdf.worker.min.js'; function initializePdf(url) { const loadingTask = getDocument(url); loadingTask.promise.then(pdf => { console.log(`Loaded ${pdf.numPages} pages`); // 初始化每一页的内容渲染... for (let i = 1; i <= pdf.numPages; i++) { renderPage(i, pdf.getPage.bind(pdf)); } }).catch(err => { console.error("Error while initializing the document", err); }); return loadingTask; } // 渲染单页内容到Canvas上 async function renderPage(num, getPageFunc){ let page = await getPageFunc(num); var viewport = page.getViewport({ scale: 1.5 }); var canvas = document.createElement('canvas'); var context = canvas.getContext('2d'); canvas.height = viewport.height; canvas.width = viewport.width; document.body.appendChild(canvas); await page.render({ canvasContext: context, viewport: viewport }).promise; }; ``` 上述代码展示了如何手动解析并显示 PDF 文档而不依赖内置 UI 组件[^2]。这种方式完全绕过了标准工具栏配置,从而自然规避了暴露敏感控件的风险。 --- #### 注意事项 尽管以上技术手段可以帮助开发者屏蔽部分外部访问途径,但从安全性角度考虑,仍需配合服务器端策略进一步加固防护措施。例如仅允许特定 IP 地址范围内的请求获取资源文件;或者动态生成带时间戳的一次性 URL 来限定生命周期等。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值