如何通过选择“将图像另存为...”来保存图像。在使用Selenium Webdriver(Python)的上下文菜单中 [英] How to save an image by selecting "save image as..." in a context menu using Selenium Webdriver (Python)

查看:261
本文介绍了如何通过选择“将图像另存为...”来保存图像。在使用Selenium Webdriver(Python)的上下文菜单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Selenium Webdriver将特定图像保存到目录中。我一直在通过模拟右键单击img元素并选择将图像另存为...来实现此目的。使用下面的代码,我可以打开上下文菜单,但无法选择正确的选项。

I'm trying to use selenium webdriver to save a specific image to a directory. I was looking to do this by simulating a right click on the img element and selecting "save image as...". With the following code I can open the context menu, but I'm unable to select the correct option.

browser = WebDriver(executable_path=CHROMEDRIVER_PATH)
browser.get(URL)
img = browser.find_element_by_tag_name('img')
ActionChains(browser).context_click(img).perform()

我也尝试过:

ActionChains(browser).context_click(img).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()                 

并使用快捷方式( v似乎选择了将图像另存为...)

and using a shortcut ('v' seems to select "save image as...")

ActionChains(browser).context_click(img).send_keys('v').perform()

图片没有直接的网址,因为它是一个验证码图片,每次点击时都会随机重新加载。对于我来说,我能够对其进行处理的唯一方法是首先将其保存在磁盘上(使用另存为...)。保存整个页面不会保存该特定图像,因此它也无法正常工作。

The image does not have a direct URL because it's a captcha image that is reloaded randomly on each click. The only way I found, for me to be able to process it, is to save it on the disk first (using "save image as..."). Saving the entire page does not save this specific image so it won't work as well.

有任何想法吗?

推荐答案

如果您要使用的是验证码,则最好还是截屏。

If it's a captcha you're after, you're probably better off just taking a screenshot.

driver.save_screenshot('screenshot.png')

这篇关于如何通过选择“将图像另存为...”来保存图像。在使用Selenium Webdriver(Python)的上下文菜单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆