from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By try: # 启动驱动程序 driver = webdriver.Chrome() print('打开网址') driver.get("https://www.baidu.com/") print('设置等待') wait = WebDriverWait(driver, 1) print('存储原始窗口的 ID') original_window = driver.current_window_handle print(original_window) print('检查一下,我们还没有打开其他的窗口') assert len(driver.window_handles) == 1 print('单击在新窗口中打开的链接') driver.find_element(By.LINK_TEXT, "新闻").click() # print('等待新窗口或标签页') # wait.until(EC.number_of_windows_to_be(2)) # print('循环执行,直到找到一个新的窗口句柄') for window_handle in driver.window_handles: if window_handle != original_window: driver.switch_to.window(window_handle) break # print('等待新标签页完成加载内容') # wait.until(EC.title_is("SeleniumHQ Browser Automation")) print('关闭标签页或窗口') driver.close() print('切回到之前的标签页或窗口') driver.switch_to.window(original_window) print('分别获取每个尺寸') width = driver.get_window_size().get("width") height = driver.get_window_size().get("height") print(width, height) print('或者存储尺寸并在以后查询它们') size = driver.get_window_size() width1 = size.get("width") height1 = size.get("height") print(width, height) print('恢复窗口并设置窗口大小') driver.set_window_size(1024, 768) print('分别获取每个尺寸') x = driver.get_window_position().get('x') y = driver.get_window_position().get('y') print(x, y) print('或者存储尺寸并在以后查询它们') position = driver.get_window_position() x1 = position.get('x') y1 = position.get('y') print(x1, y1) # 将窗口移动到主显示器的左上角 driver.set_window_position(0, 0) print('最大化窗口') driver.maximize_window() print('最小化窗口') driver.minimize_window() print('填充整个屏幕,类似于在大多数浏览器中按下 F11') driver.fullscreen_window() print('捕获当前浏览上下文的屏幕截图') driver.save_screenshot('./image_fullscreen.png') print('捕获当前浏览上下文的元素的屏幕截图') ele = driver.find_element(By.CSS_SELECTOR, 'textarea') print('Returns and base64 encoded string into image') ele.screenshot('./image_element.png') finally: print('在会话结束时退出浏览器') driver.quit()
browser main pulation 02
最新推荐文章于 2024-09-11 17:14:57 发布