python selenium 右键另存为 爬坑指南 (包含windows和linux)

本文所有实现都基于selenium + 火狐浏览器,及geckodriver驱动 ,驱动下载地址https://github.com/mozilla/geckodriver/releases,找到对应系统的版本,注意驱动还需要和浏览器的版本配对,一般更新浏览器到最新,然后用最新的驱动。不然会报如下的错:

selenium.common.exceptions.WebDriverException: Message: Unable to find a matching set of capabilities

意思就是浏览器版本与驱动不匹配。linux系统需要在软件中心更新Firefox,不建议自己从火狐官网下安装包

目前右键另存为的方式很多都是如下

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

action = ActionChains(driver).move_to_element(element)#移动到该元素
action.context_click(element)#右键点击该元素
action.send_keys(Keys.ARROW_DOWN)#点击键盘向下箭头
action.send_keys('v')#键盘输入V保存图
action.perform()#执行保存

然而,并木有用,右键是实现了,但无法按出v以及保存。原因是selenium自带的send_keys是在选中某个元素后,给该元素输入键盘上的按键,无法选中系统的弹窗,所以当右键菜单跳出来以后selenium就无能为力了。

针对该问题,目前主流的方法是安装python模拟键盘的库,例如windows的pypiwin32,linux的virtkey和PyUserInput。

Windows:

    亲测python3.5 使用pip install pypiwin32 安装成功,

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
import win32api
import win32con
VK_CODE ={'enter':0x0D, 'down_arrow':0x28}
#键盘键按下
def keyDown(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, 0, 0)
#键盘键抬起
def keyUp(keyName):
    win32api.keybd_event(VK_CODE[keyName], 0, win32con.KEYEVENTF_KEYUP, 0)

print('Please wait...Firefox loading...')
print('---------------------------------')
# 用浏览器实现访问
driver = webdriver.Firefox()
driver.get("一个url")
element = driver.find_element_by_id('img').find_element_by_tag_name('img')
img_url = element.get_attribute('src')
#print(img_url)
action = ActionChains(driver).move_to_element(element)
action.context_click(element).perform()
time.sleep(1)
#按v
win32api.keybd_event(86, 0, 0, 0)
win32api.keybd_event(86, 0, win32con.KEYEVENTF_KEYUP, 0)
time.sleep(1)
#按enter
keyDown("enter")
keyUp("enter")
time.sleep(1)

这里https://www.cnblogs.com/wumac/p/5994923.html 列出了一些按键的代码

Linux:

   亲测Ubuntu64自带的python2.7.6使用 sudo apt-get install python-virtkey 安装成功https://www.howtoinstall.co/en/ubuntu/trusty/universe/python-virtkey/,virtkey仅支持python2.7,并能显示在pip list中;但是,python2.7.6安装requests会有问题,报错chardet无法卸载,需要更新python版本;而当我把python升级到2.7.13后,requests是装上了,但virtkey安装完毕后没显示在pip list中,也无法import,所以放弃了。下面列出了virtkey 的按键代码https://blog.csdn.net/zhouy1989/article/details/13997507

下面,我们采用了PyUserInput库,它集合了

  • PyMouse, 专门模拟鼠标操作
  • PyKeyboard,专门模拟键盘上的操作

安装方法: pip install PyUserInput (注意要Python2.7)

from pykeyboard import PyKeyboard
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Key

driver = webdriver.Firefox()
k = PyKeyboard()
driver.get("一个url")
element = driver.find_element_by_id('img').find_element_by_tag_name('img')
img_url = element.get_attribute('src')
action = ActionChains(driver).move_to_element(element).context_click(element).perform()
time.sleep(1)
k.tap_key("v")
time.sleep(1)
k.tap_key(k.enter_key) #Enter
time.sleep(1)

这里给出PyUserInput模块的使用方法 https://github.com/PyUserInput/PyUserInput

总结:

我们只是用selenium实现了右键,后面的操作由其他库来模拟键盘完成;windows相对方便一点,pypiwin32对python的各个版本兼容比较好,但是windows系统稳定性较差,需要留足sleep时间;linux比较麻烦,只能用python2.7,不然很多库装不上,

PyUserInput比较好用,linux系统稳定性好;建议在虚拟机上运行,不然,你就不能用电脑了,因为你的系统始终在模拟按键以及选中element,有虚拟机你就可以放心切出去做其他事了;最后, 考虑到网络问题,有时候会有timeoutexception,建议做一个retry。

  • 11
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
引用\[1\]和\[2\]提供了使用PythonSelenium库进行模拟鼠标右键点击的示例代码。你可以根据这些示例代码来实现你的需求。首先,你需要安装Selenium库,并选择一个浏览器驱动程序(如Chrome或Firefox)。然后,你可以使用`ActionChains`类来执行鼠标右键点击操作。以下是一个示例代码: ```python from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() # 使用Chrome浏览器驱动 driver.get("https://example.com") # 打开一个网页 element = driver.find_element_by_xpath("//xpath/to/element") # 定位到要右键点击的元素 actions = ActionChains(driver) actions.context_click(element).perform() # 执行鼠标右键点击操作 driver.quit() # 关闭浏览器 ``` 你可以根据你的具体需求修改代码中的网页地址和元素定位方式。希望这个示例能帮到你。 #### 引用[.reference_title] - *1* [python+selenium小结11:模拟鼠标右键并在右键菜单上选择](https://blog.csdn.net/jusulysunbeamy/article/details/90003194)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [selenium-webdriver(python) -- 鼠标事件(双击,右键)](https://blog.csdn.net/xiaodanpeng/article/details/50999026)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值