Automation with Chrome, Selenium, PyAutoGUI using Python.
In today’s world, Web scraping plays a vital role in Data Collection. Where in, we do scraping of texts and images from website as part of the data collection and data wrangling process.
Scraping text has been made very easy, with many available packages like Beautiful Soup,Scrapy, Selenium web driver which has tons of tutorial on how to use them.
Often, images are results of converting or capturing a web-page as screenshots using the selenium web driver. Selenium is a wonderful tool which helps in many web scraping, testing and automation tasks. But it has few limitations,as an user can control only the browser’s Display Window via code but not other sections of the browser like Address Bar, Tools Bar etc. example, a web driver cannot trigger a click event on browser extensions - as the mouse pointer has been restricted with in the browser’s Display Window.
Load Chrome Browser with Extensions and Invoke a click event, using Python
In certain scenarios, we need browser extensions to do few steps for us to complete the data collection activity. Also many automation scripts require clicking on different locations in a browser. Let us see how to achieve this using python.
A simple approach in python: A) Load the Chrome with browser Extensions using Selenium web driver. B) use PyAutoGUI to locate and click on the extension
What is PyAutoGUI ? A python module for programmatically controlling the mouse and keyboard, used frequently for many automation scripts
Click here to refer the various installing method for PyAutoGUI
How it works? PyAutoGUI locates the target image (declared as parameter) in the current active window/screen, also triggers a click event on it.
Programmatic Flow: Given a scenario as mentioned in the post to trigger a click event on a chrome extension. To do so, A) Save the extension icon as an image. B)Locate the extension by passing the image as a parameter to pyautogui.locateOnScreen(“image.png”) method, which records the X,Y coordinates of the located extension. C) Trigger click event by calling the pyautogui.click() method and then pass the recorded X,Y coordinates along with additional parameters like interval, type of click either right/left as shown below.
Reference ::http://pyautogui.readthedocs.io/en/latest/cheatsheet.html, https://realpython.com/tutorials/scraping/