def capture_element_screenshot(driver: webdriver, selector: str, output_file: str) -> None:
时间: 2024-02-06 21:02:14 浏览: 183
这里需要将 `webdriver` 改成 `WebDriver`,并且需要导入 `webdriver` 模块:
```python
from selenium.webdriver.chrome.webdriver import WebDriver
def capture_element_screenshot(driver: WebDriver, selector: str, output_file: str) -> None:
# 等待元素出现
driver.implicitly_wait(10)
element = driver.find_element(By.CSS_SELECTOR, selector)
# 获取元素在页面中的位置和大小
left, top, width, height = element.location['x'], element.location['y'], element.size['width'], element.size['height']
right, bottom = left + width, top + height
# 截取整个页面
driver.save_screenshot(output_file)
# 裁剪出指定元素对应的图片
image = Image.open(output_file)
element_image = image.crop((left, top, right, bottom))
element_image.save(output_file)
```
阅读全文