disable browser back button after logout

本文探讨了深度学习在图像处理领域的应用,包括AR特效、图像处理、音视频直播等场景,展示了深度学习如何提升图像处理的效率与效果。
部署运行你感兴趣的模型镜像

 

<h:head>
	<title>
	      <ui:insert name="title">Title</ui:insert>
	</title>
	<meta http-equiv="CACHE-CONTROL" content="NO-CACHE"/>
	<meta http-equiv="PRAGMA" content="NO-CACHE"/>
	<meta http-equiv="expires" content="0" />
	<meta name="ROBOTS" content="NONE"/> 
	<meta name="GOOGLEBOT" content="NOARCHIVE"/>
	<script>history.forward();</script>
</h:head>
 

 

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

这是我的原始代码,可以正常运行没有任何问题 from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.edge.service import Service 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 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.common.action_chains import ActionChains from selenium.webdriver.edge.service import Service as EdgeService class SvelteKitAutomator: clickable_selectors = ( 'button, a, input[type="button"], input[type="submit"], ' 'div[role="button"], div[onclick], [role="menuitem"], ' '[role="option"], [data-testid*="button"], [class*="btn"], ' '[class*="clickable"]' ) input_selectors = ( 'input[type="text"], input[type="email"], input[type="number"], ' 'input[type="password"], input[type="tel"], input[type="url"], ' 'textarea, [role="textbox"], [contenteditable="true"]' ) def __init__(self, browser_type='edge', headless=False, login_url="http://10.244.1.179:3000/"): self.browser_type = browser_type.lower() self.headless = headless self.login_url = login_url self.options = { 'chrome': ChromeOptions(), 'edge': EdgeOptions(), 'firefox': FirefoxOptions() }.get(self.browser_type) if self.options is None: raise ValueError(f"Unsupported browser: {self.browser_type}") if self.headless: if self.browser_type == 'chrome': self.options.add_argument('--headless=new') self.options.add_argument('--disable-gpu') elif self.browser_type == 'edge': self.options.add_argument('--headless=chrome') elif self.browser_type == 'firefox': self.options.add_argument('--headless') driver_paths = { 'edge': r"C:\own\app\python_code\work\入职培训\打印点击\msedgedriver.exe" } driver_path = driver_paths.get(self.browser_type) if self.browser_type == 'chrome': self.driver = webdriver.Chrome(options=self.options) elif self.browser_type == 'firefox': self.driver = webdriver.Firefox(options=self.options) elif self.browser_type == 'edge': if not driver_path: raise ValueError("Edge driver path is required.") service = EdgeService(driver_path) self.driver = webdriver.Edge(service=service, options=self.options) else: raise ValueError(f"Unsupported browser: {self.browser_type}") self.action = ActionChains(self.driver) self.driver.get(self.login_url) print(f"Browser {self.browser_type} started in {'headless' if self.headless else 'headed'} mode.") 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 login(self, email_1, password_1): try: email_input = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.NAME, 'email')) ) email_input.send_keys(email_1) password_input = WebDriverWait(self.driver, 10).until( EC.presence_of_element_located((By.NAME, 'current-password')) ) password_input.send_keys(password_1) 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): user_button =WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='User Menu']")) ) self.safe_click(user_button) logout_button = self.driver.find_element(By.XPATH, "//div[text()='サインアウト']") self.safe_click(logout_button) print("登出成功") def click_all_clickable_elements(self): try: elements_locator = (By.CSS_SELECTOR, SvelteKitAutomator.clickable_selectors) 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.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, SvelteKitAutomator.input_selectors) 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.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 safe_click(self, element): try: element.click() except ElementClickInterceptedException: print("元素被遮挡,尝试使用 JavaScript 点击") self.driver.execute_script("arguments[0].click();", element) #写死按钮位置点击设置按钮 def click_settings_menu_items(self): try: advanced_settings_button = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Controls']"))) self.safe_click(advanced_settings_button) print("已点击“高级对话设置”按钮") try: container = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//div[text()='高度なパラメータ']")) ) print("已找到高级参数容器") try: buttons = WebDriverWait(self.driver, 10).until( EC.presence_of_all_elements_located( (By.XPATH, "//div[@class='space-y-1 text-xs pb-safe-bottom']//button")) ) print(f"找到 {len(buttons)} 个按钮") for index, button in enumerate(buttons): try: if button.is_displayed() and button.is_enabled(): button.click() print(f"已点击按钮 {index + 1}") time.sleep(0.2) else: print(f"按钮 {index + 1} 未显示或不可点击,跳过") except Exception as e: print(f"点击按钮 {index + 1} 时出错: {str(e)}") except Exception as e: print(f"点击参数时出错: {str(e)}") except Exception as e: print(f"遍历设置项时出错: {str(e)}") except Exception as e: print(f"点击设置菜单项时出错: {str(e)}") #获取页面前后变化点击设置按钮 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() 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() 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 def click_settings_by_change(self): #手动定位设置 # 点击“高级对话设置”按钮 advanced_settings_button = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Controls']")) ) self.safe_click(advanced_settings_button) print("已展开“高级对话设置”按钮") link = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//div[text()='高度なパラメータ']")) ) new_buttons = self.click_and_detect_changes(link) if new_buttons: print(f"找到 {len(new_buttons)} 个新增按钮:", new_buttons) for btn in new_buttons: try: self.safe_click(btn) print(f"已点击按钮: {btn.text or '未知按钮'}") time.sleep(0.5) except Exception as e: print(f"点击按钮时出错: {str(e)}") else: print("未检测到新增按钮") #收起列表防止遮挡 advanced_settings_button = WebDriverWait(self.driver, 10).until( EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Controls']")) ) self.safe_click(advanced_settings_button) print("已收起“高级对话设置”按钮") print("自动化点击设置按钮完成") def run_automation(self, email_1, password_1): try: login_success = self.login(email_1, password_1) if not login_success: print("登录失败,无法继续自动化操作") return print("登录成功,开始自动化操作...") # self.click_all_clickable_elements() self.handle_input_fields() # time.sleep(2) # self.click_settings_menu_items()#写死settings self.click_settings_by_change()#自动settings self.logout() print("自动化操作完成") except Exception as e: print(f"自动化过程中发生错误: {str(e)}") finally: time.sleep(5) self.driver.quit() if __name__ == "__main__": login_url="http://10.244.1.179:3000/" # login_url_1="https://baike.baidu.com/item/%E5%91%A8%E6%9D%B0%E4%BC%A6/129156" email_1 = "shengkai.qu@brother-bsh.com.cn" password_1 = "q123456.+" automator = SvelteKitAutomator(headless=False) automator.run_automation(email_1, password_1) 这是我对原始代码的优化,产生的问题 1.会多打开1个data;; 2.设置键找不到,该页面的设置在(//*[@id="M7InACbqn5"]/button[1]/div[2],这只是示例,要通用),但是不在父元素下 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
问题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
想用selenium+python实现遍历点击页面所有可点击的元素(按钮,输入框(处理输入框由使用者自行输入判断,把输入框高亮),选择/下拉按钮),对元素点击造成的跳转后的页面也要进行处理(处理完跳转页面后返回上一界面点击上一界面未点击的直到所有页面的内容都被点击过,注意可能会陷入套娃死循环),考虑页面是否是动态加载或者静态,要对已点击的元素进行记录避免二次点击,注意有些元素加载后可能不在父元素下,我要即插即用,所有页面都可进行处理 示例页面https://www.aitranslatenow.com/zh/,http://10.244.1.179:3000/ 我的代码: 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': self.options.add_argument('--headless=new') self.options.add_argument('--disable-gpu') elif self.browser_type == 'edge': self.options.add_argument('--headless=chrome') elif self.browser_type == 'firefox': self.options.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() 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 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) 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() if login_required and self.login_url:#判断是否要登出 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-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值