You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Selenium WebDriver打开新标签页并完成复制粘贴操作?

使用Selenium WebDriver实现新标签页操作(复制-关闭-粘贴流程)

以下是完整的实现步骤和代码示例,基于你提供的初始代码扩展:

核心流程概述

打开新标签页 → 切换到新标签页 → 获取目标内容 → 关闭新标签页 → 切回原标签页 → 粘贴内容


1. 导入必要模块

除了你已有的代码,还需要导入几个常用模块来处理元素等待、操作链等:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import platform

2. 打开新标签页

推荐使用执行JavaScript的方式,比键盘快捷键更稳定,支持指定URL或空白标签页:

# 打开空白新标签页
driver.execute_script("window.open('');")

# 或者直接打开指定URL的新标签页
# driver.execute_script("window.open('https://example.com');")

3. 切换到新标签页

Selenium通过**窗口句柄(window handles)**管理标签页,需要切换到新打开的标签页:

# 获取所有标签页的句柄列表
window_handles = driver.window_handles
# 切换到最新打开的标签页(列表最后一个元素)
driver.switch_to.window(window_handles[-1])

4. 在新标签页获取并复制内容

有两种常见方式获取内容:直接提取文本到变量,或模拟系统剪贴板复制:

方式1:直接提取文本(更高效)

# 等待目标元素加载完成,提取文本
target_element = wait.until(EC.presence_of_element_located((By.ID, "target-content")))
copied_text = target_element.text

方式2:模拟剪贴板复制(适合富文本或依赖剪贴板的场景)

# 适配不同系统的快捷键(Mac用COMMAND,Windows/Linux用CONTROL)
ctrl_key = Keys.COMMAND if platform.system() == 'Darwin' else Keys.CONTROL

# 选中元素文本并执行复制操作
ActionChains(driver)\
    .move_to_element(target_element)\
    .double_click()\
    .key_down(ctrl_key)\
    .send_keys('c')\
    .key_up(ctrl_key)\
    .perform()

5. 关闭新标签页并切回原标签页

关闭当前标签页后,切换回初始标签页:

# 关闭新标签页
driver.close()
# 切换回原标签页(列表第一个元素)
driver.switch_to.window(window_handles[0])

6. 在原标签页粘贴内容

对应复制方式,粘贴也有两种方式:

方式1:使用变量直接输入(对应直接提取文本)

# 等待输入框加载完成,清除原有内容后粘贴
input_box = wait.until(EC.presence_of_element_located((By.ID, "input-box")))
input_box.clear()
input_box.send_keys(copied_text)

方式2:模拟剪贴板粘贴(对应剪贴板复制)

# 再次使用适配的快捷键
ctrl_key = Keys.COMMAND if platform.system() == 'Darwin' else Keys.CONTROL

# 点击输入框并执行粘贴操作
ActionChains(driver)\
    .move_to_element(input_box)\
    .click()\
    .key_down(ctrl_key)\
    .send_keys('v')\
    .key_up(ctrl_key)\
    .perform()

完整代码示例

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
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import platform

# 初始代码
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 30)

# 先打开原页面示例
driver.get("https://your-original-page.com")

# 1. 打开新标签页(指定目标URL)
driver.execute_script("window.open('https://example.com');")

# 2. 切换到新标签页
window_handles = driver.window_handles
driver.switch_to.window(window_handles[-1])

# 3. 获取并复制内容(示例提取页面标题)
target_element = wait.until(EC.presence_of_element_located((By.TAG_NAME, "h1")))
copied_text = target_element.text

# 4. 关闭新标签页,切回原标签页
driver.close()
driver.switch_to.window(window_handles[0])

# 5. 粘贴内容到原页面输入框
input_box = wait.until(EC.presence_of_element_located((By.ID, "input-area")))
input_box.clear()
input_box.send_keys(copied_text)

# 结束操作
# driver.quit()

注意事项

  • 切换标签页时,务必确认窗口句柄的顺序,避免切换到错误标签页
  • 始终使用WebDriverWait等待元素加载,避免因页面未渲染完成导致的NoSuchElementException
  • 跨平台使用快捷键时,记得判断操作系统类型,避免快捷键失效

内容的提问来源于stack exchange,提问作者container

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 06:30:45