I am working with the R and Python languages.
Suppose I search for the following Canadian Postal Code (M5V 3L9) on Google Maps:
When I search for this, I can see that the "perimeter" of this Postal Code is highlighted in red:
My Question: (Using Selenium via R/Python) From an HTML/CSS/XML perspective – I am trying to get a list of all coordinates that make up the boundary of this perimeter.
I have been trying to explore the source code that is generated from this website to try and see if there is something I can do to see where the source code of this perimeter (e.g. in JSON) is being stored – but so far, I can’t find anything:
I was hoping that perhaps there might be something which would allow me to use Selenium to repeatedly click around this perimeter and extract the longitude/latitude points – but so far, I can not find anything.
Can someone please show me how to do this?
Thanks!
Note: Generic Selenium Code:
library(RSelenium)
library(wdman)
library(netstat)
selenium()
seleium_object <- selenium(retcommand = T, check = F)
remote_driver <- rsDriver(browser = "chrome", chromever = "114.0.5735.90", verbose = F, port = free_port())
remDr<- remote_driver$client
remDr$navigate("https://www.google.com/maps/place/Toronto,+ON+M5V+3L9/@43.642566,-79.3875851,18z/data=!4m6!3m5!1s0x882b34d436f9c825:0x9e9c6195e38030f2!8m2!3d43.6429129!4d-79.3853443!16s%2Fg%2F1tvq4rqd?entry=ttu")
2
Answers
You could use the PyAutoGui library to look on the screen for the HEX value of the red outline, then move the mouse to that point, right-click, then use another library’s text recognition (like pytesseract) to scan the latitude and longitude coords that appear in the right-click menu. I’m not sure about the text recognition, but the PyAutoGui part is very easy to implement, around 10 lines of code. This is an example of how you could implement this:
And here is how to do part of it with Selenium:
From my understanding, you cannot get the rgb value of an individual pixel with selenium, but you can get the rgb value of an element. This means we wont be able to find the rgb value of the pixels we want to move our cursor to (the red boundary). Installing pyautogui would be much easier (
pip install pyautogui
)library(RSelenium)
library(wdman)
library(netstat)
selenium()
selenium_object <- rsDriver(browser = "chrome", chromever = "114.0.5735.90", verbose = FALSE, port = free_port())
remDr <- selenium_object$client
remDr$navigate("https://www.google.com/maps/place/Toronto,+ON+M5V+3L9/@43.642566,-79.3875851,18z/data=!4m6!3m5!1s0x882b34d436f9c825:0x9e9c6195e38030f2!8m2!3d43.6429129!4d-79.3853443!16s%2Fg%2F1tvq4rqd?entry=ttu"😉