【问题标题】:Automatically substituting logos in web images自动替换 Web 图像中的徽标
【发布时间】:2019-02-25 15:15:05
【问题描述】:
考虑以下任务:
- 打开给定的 URL
- 在 URL 中找到第一个图像标记
- 用它替换本地驱动器中的图像
- 将生成的网页另存为 png
我想使用 Python 脚本自动执行此任务,但我不确定最佳方法。
我一直在用selenium把url转成截图,但是不知道怎么介绍修改第一个图片标签来加载本地文件的部分。
【问题讨论】:
标签:
python
selenium
automation
【解决方案1】:
您可以使用execute_script 来替换图像应该如下所示:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
url = 'https://www.aircanada.com/en/'
browser.get(url)
my_image = browser.find_element_by_xpath('//*[@id="pagePromoBanner-wrapper"]/div/a/img')
# or
# my_image = browser.find_element_by_xpath('any XPath')
link_to_new_image = "https://images.pexels.com/photos/67636/rose-blue-flower-rose-blooms-67636.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260"
# if you are using python 3.6 and up:
browser.execute_script(f"arguments[0].src = '{link_to_new_image}'", my_image )
# else:
# browser.execute_script("arguments[0].src = '"+link_to_new_image+"'", my_image )
希望对您有所帮助!