skip to Main Content

I am working with the R and Python languages.

Suppose I search for the following Canadian Postal Code (M5V 3L9) on Google Maps:

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

When I search for this, I can see that the "perimeter" of this Postal Code is highlighted in red:

enter image description here

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:

enter image description here

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


  1. 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:

    import pyautogui
    
    def find_and_right_click(color):
        screen_width, screen_height = pyautogui.size()
        for x in range(screen_width):
            for y in range(screen_height):
                pixel_color = pyautogui.pixel(x, y)
                if pixel_color == color:
                    pyautogui.moveTo(x, y)
                    pyautogui.rightClick()
    
    target_color = (234, 68, 54)
    
    find_and_right_click(target_color)
    

    And here is how to do part of it with Selenium:

    from selenium import webdriver
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver = webdriver.Chrome()
    actions = ActionChains(driver)
    actions.context_click(element).perform() #right click
    

    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)

    Login or Signup to reply.
  2. 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&quot😉

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search