[Python] Right-click Selenium to Save the picture error: attributeerror: solution to module ‘pyscreen’ has no attribute ‘locationonwindow’

When crawling pictures with selenium module, simulate right clicking and select “save picture as” to save the picture (pyautogui module is not used)

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

url = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201202%2F18%2F20120218194349_ZHW5V.thumb.700_0.jpg&refer=http%3A%2F%2Fcdn.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633348528&t=794e58b55dfe37e5f3082568feb89783'

driver = webdriver.Chrome()
driver.get(url)
element = driver.find_element_by_xpath('/html/body/img[1]')  

action = ActionChains(driver)  
action.move_to_element(element)  
action.context_click(element)  
action.send_keys(Keys.ARROW_DOWN)  
action.send_keys('v')  

action.perform()  
sleep(2)

driver.quit()  

When you execute the above code, you will find that you do not click the keyboard after right clicking, because the page is not controlled by selenium after right clicking. At this point, we need to use the pyautogui module, which is a module for automatically controlling the mouse and keyboard
you need to PIP download the wheel first

pip install pyautogui

Modified code:

from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from time import sleep
import pyautogui

url = 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fcdn.duitang.com%2Fuploads%2Fitem%2F201202%2F18%2F20120218194349_ZHW5V.thumb.700_0.jpg&refer=http%3A%2F%2Fcdn.duitang.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633348528&t=794e58b55dfe37e5f3082568feb89783'

driver = webdriver.Chrome()
driver.get(url)
element = driver.find_element_by_xpath('/html/body/img[1]') 

action = ActionChains(driver) 
action.move_to_element(element) 
action.context_click(element) 
action.perform() 

pyautogui.typewrite(['v']) 
sleep(1) 
pyautogui.typewrite(['1', '.', 'j', 'p', 'g'])
pyautogui.typewrite(['enter'])

sleep(1)

driver.quit() 

The red explosion will be found after execution:


attributeerror: module ‘pyscreen’ has no attribute ‘locateonwindow’

It is actually very simple to solve this problem
we directly download the highest version 0.9.53 after PIP install pyautogui
we only need to download version 0.9.50, so this problem will not exist

pip install pyautogui==0.9.50

Just execute the code again~


Read More: