【Python爬虫】selenium4新版本使用指南
发布日期:2021-05-16 00:08:23 浏览次数:33 分类:精选文章

本文共 3458 字,大约阅读时间需要 11 分钟。

Selenium 4驱动设置与元素操作指南

Selenium 是一个强大的 Web 应用测试工具,其核心功能是模拟用户在浏览器中进行操作。通过 Selenium,可以对不同浏览器和操作系统进行兼容性测试,确保应用程序在各种环境下都能正常运行。本文将详细介绍 Selenium 4 的新特性,特别是驱动设置和元素操作的方法。

1. 驱动设置

Selenium 4 引入了新的驱动管理方式,相比传统方法更加简便。以下是两种主要方法:

1.1 使用 Webdriver Manager

Webdriver Manager 提供了一个方便的管理器,可以自动下载和配置驱动。安装命令如下:

pip install webdriver-manager

然后导入必要的包:

from webdriver_manager.chrome import ChromeDriverManager

创建服务并安装驱动:

service = ChromeService(executable_path=ChromeDriverManager().install())

最后初始化驱动:

driver = webdriver.Chrome(service=service)

完整代码示例:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
service = ChromeService(executable_path=ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.quit()

1.2 传统方法

传统方法仍然支持,但需要注意以下几点:

  • 导入 Service 包:

    from selenium.webdriver.chrome.service import Service as ChromeService
  • 设置驱动路径:

    CHROMEDRIVER_PATH = "path/to/chromedriver"
    service = ChromeService(executable_path=CHROMEDRIVER_PATH)
  • 初始化驱动:

    driver = webdriver.Chrome(service=service, options=options)

完整代码示例:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service, options=options)
driver.quit()

2. 元素查找

Selenium 4 对元素查找方法进行了优化,所有查找方法都通过 find_element 实现,并接受 By 类方法参数。

2.1 常用查找方法

  • 根据类名查找:

    driver.find_element(By.CLASS_NAME, "className")
  • 根据 CSS 选择器查找:

    driver.find_element(By.CSS_SELECTOR, ".className")
  • 根据 ID 查找:

    driver.find_element(By.ID, "elementId")
  • 根据 XPath 查找:

    driver.find_element(By.XPATH, "path")
  • 根据可见文本查找:

    driver.find_element(By.VISIBLE, "visible text")
  • 根据部分可见文本查找:

    driver.find_element(By.PARTIAL_VISIBLE, "partial visible text")
  • 根据标签名查找:

    driver.find_element(By.TAG_NAME, "tag")

2.2 等待元素

为了避免元素未加载导致错误,可以使用等待方法:

from selenium.webdriver.support.ui import WebDriverWait
# 等待 3 秒直到元素加载
el = WebDriverWait(driver, timeout=3).until(lambda d: d.find_element_by_tag_name("p"))

3. 动作 API

Selenium 的动作 API 提供了更丰富的操作方式,适用于复杂场景。

3.1 暂停操作

在执行动作时,可以暂停一定时间:

ac = ActionChains(driver)
ac.move_to_element(clickable).pause(1).click_and_hold().pause(1).send_keys("abc").perform()

3.2 键盘操作

  • 按下键盘组合:

    ac = ActionChains(driver)
    ac.key_down(Keys.SHIFT).send_keys("a").key_up(Keys.SHIFT).send_keys("b").perform()
  • 输入字符串:

    text_input = driver.find_element(By.ID, "textInput")
    ac.send_keys_to_element(text_input, "abc").perform()

3.3 鼓励操作

  • 鼓励点击:

    clickable = driver.find_element(By.ID, "clickable")
    ac.click_and_hold(clickable).perform()
  • 鼓励双击:

    ac.double_click(clickable).perform()
  • 鼓励右键:

    ac.context_click(clickable).perform()

3.4 鼓励拖拽

  • 拖拽元素:

    draggable = driver.find_element(By.ID, "draggable")
    droppable = driver.find_element(By.ID, "droppable")
    ac.drag_and_drop(draggable, droppable).perform()
  • 拖拽到特定位置:

    start = draggable.location
    finish = droppable.location
    ac.drag_and_drop_by_offset(draggable, finish['x'] - start['x'], finish['y'] - start['y']).perform()

3.5 滚轮操作

  • 滚动到元素:

    iframe = driver.find_element(By.TAG_NAME, "iframe")
    ac.scroll_to_element(iframe).perform()
  • 定量滚动:

    footer = driver.find_element(By.TAG_NAME, "footer")
    delta_y = footer.rect['y']
    ac.scroll_by_amount(0, delta_y).perform()

总结

以上就是本文的完整内容,涵盖了 Selenium 4 的驱动设置、元素查找和动作操作的最新方法。希望对您有所帮助!

上一篇:No.3.1_11 JavaSE入门 P10 【常用API】数组排序和Arrays工具类、包装类、Date
下一篇:No.3.1_8 JavaSE入门 P7 【面向对象】继承&多态

发表评论

最新留言

做的很好,不错不错
[***.243.131.199]2025年05月21日 06时35分19秒