如何在 selenium 中右键单击并单击将图像另存为 python [英] How to right click in selenium and click save image as in python

查看:83
本文介绍了如何在 selenium 中右键单击并单击将图像另存为 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用鼠标右键单击,然后在 selenium python 中单击另存为图像.我能够使用以下方法执行右键单击,但是执行右键单击的下一个操作不再起作用.我该如何解决这个问题?

I am trying to right click with mouse and click save as Image in selenium python. I was able to perform right click with follwing method, however the next action to perform right click does not work any more. How can I solve this problem?

from selenium.webdriver import ActionChains 
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
driver.get(url)

    # get the image source
img = driver.find_element_by_xpath('//img')
actionChains = ActionChains(driver)
actionChains.context_click(img).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ARROW_DOWN).send_keys(Keys.RETURN).perform()

推荐答案

问题是 send_keys() 方法在创建上下文菜单后,将键发送到窗口,而不是菜单.因此,无法访问菜单项.

The problem is that the send_keys() method, after having created the context menu, sends the keys to the window, and not to the menu. So, there is no way to access the menu items.

我在下载在网页中创建的画布时遇到了类似的问题.最后,我能够下载执行 javascript 的图像.我创建了一个下载元素来管理图像.由于它是一个画布,我之前必须执行 toDataURL 方法.这是我的python代码:

I had a similar problem with downloading a canvas created in a webpage. Finally, I was able to download the image executing a javascript. I created a download element in order to manage the image. As it was a canvas, I had previously to execute the toDataURL method. Here is my python code:

script_js = 'var dataURL = document.getElementsByClassName("_cx6")[0].toDataURL("image/png");' \
    'var link = document.createElement("a"); ' \
    'link.download = "{}_{}";' \
    'link.href = dataURL;' \
    'document.body.appendChild(link);' \
    'link.click();' \
    'document.body.removeChild(link);' \
    'delete link;'.format( n, prefijo_nombre_archivo, sufijo_nombre_archivo )
driver.execute_script(script_js)

希望能帮到你!

这篇关于如何在 selenium 中右键单击并单击将图像另存为 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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