前言
右键保存,网上找到了一些代码,执行后,没效果
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
chromedriver = "D:\python3.6\Scripts\chromedriver.exe"
driver = webdriver.Chrome(chromedriver)
#超链接元素定位
element = '//*[@id="Dialog"]/p[1]/span[2]/a'
#通过ActionChains的context_click进行对link元素右键操作,在按下另存为的快捷键K
ActionChains(driver).context_click(link).send_keys('K').perform()
这里有两种方法可以试试,我测试都成功了
一、第一种方法:win32api
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import win32api
import win32con
print('Please wait...Firefox loading...')
print('---------------------------------')
# 用浏览器实现访问
driver = webdriver.Chrome(executable_path="chromedriver")
url = "https://www.cnhnb.com/hangqing/"
driver.get(url)
element = driver.find_elements_by_class_name('verify-sub-block')[0]
# print(img_url)
action = ActionChains(driver).move_to_element(element)
action.context_click(element).perform()
time.sleep(1)
# 按v
win32api.keybd_event(86, win32con.KEYEVENTF_KEYUP, 0) # 75的含义就是键盘的K
time.sleep(1)
二、第二种方法:pyautogui
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import pyautogui
driver = webdriver.Chrome('chromedriver.exe') # Optional argument, if not specified will search path.
driver.get('https://www.cnhnb.com/hangqing/');
# 选择元素
element = driver.find_elements_by_class_name('verify-sub-block')[0]
action = ActionChains(driver).move_to_element(element)
action.context_click(element).perform()
time.sleep(1)
# 下载滑块图片
pyautogui.typewrite(['down', 'enter'])
element2 = driver.find_elements_by_class_name('verify-img-canvas')[0]
action = ActionChains(driver).move_to_element(element2)
action.context_click(element2).perform()
time.sleep(1)
# 下载背景图片
pyautogui.typewrite(['down', 'enter'])
总结
都实现了,具体用哪种,自己看着办好了