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:
- [Solved] Python Selenium Error: AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_xpath‘
- [Solved] AttributeError: module ‘selenium.webdriver‘ has no attribute ‘Chrome‘
- [Solved] Selenium python send_key error: list object has no attribute
- [Solved] AttributeError: ‘DataParallel‘ object has no attribute ‘save‘
- How to Solve Python AttributeError: ‘module’ object has no attribute ‘xxx’
- [Solved] django AttributeError: ‘UserComment‘ object has no attribute ‘save‘
- [How to Solve]AttributeError: module ‘scipy’ has no attribute ‘io’
- [Solved] AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘
- [Solved] AttributeError: module ‘logging‘ has no attribute ‘Handler‘
- AttributeError: module ‘enum‘ has no attribute ‘IntFlag‘ [How to Solve]
- [Solved] AttributeError: module ‘tensorflow‘ has no attribute ‘distributions‘
- [Solved] AttributeError: module ‘distutils‘ has no attribute ‘version‘
- [Solved] AttributeError: module ‘setuptools._distutils‘ has no attribute ‘version‘
- AttributeError: module ‘time‘ has no attribute ‘clock‘ [How to Solve]
- [Solved] AttributeError: module ‘pandas‘ has no attribute ‘rolling_count‘
- [Solved] AttributeError: partially initialized module ‘xlwings’ has no attribute ‘App’
- [Solved] AttributeError: module ‘time‘ has no attribute ‘clock‘
- [Solved] AttributeError: module ‘PIL.Image‘ has no attribute ‘open‘
- Python AttributeError: module ‘tensorflow‘ has no attribute ‘InteractiveSession‘
- BlazingSQL Error: AttributeError: module ‘cio‘ has no attribute ‘RunQueryError‘