How to interact with map elements in Selenium

Issue

I’ve recently started to work with the Selenium Chrome driver in Python, and I ran into an issue when trying to interact with a NASA map interface. I aim to follow Curiosity’s route by iterating and centering over all the waypoints on the map chronologically. The best solution I was able to find is to use a toolbar element, as this element’s behaviour centres the map to a given waypoint on click.

However, I am unsure how to use Selenium for this purpose, as it seems you first have to mouse click on a specific waypoint to trigger the toolbar element, and then this element centres the screen on the waypoint. The ideal scenario would be for me to edit the toolbar element programmatically, to iterate over all waypoints in a given integer range.

Currently, I was able to find the toolbar element and click it, but I am unsure how to change the waypoint ID to which it is pointing. If this proves too difficult, I am open to alternative solutions.

Interface example:

Example

Current implementation:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

import time

driver = webdriver.Chrome()
driver.get("https://mars.nasa.gov/maps/location/?mission=MSL&site=NOW")

time.sleep(5)  # to allow for page loading

# the ID for the toolbar element
elem = driver.find_element_by_id("mainDescPointInner") 
elem.click()

for i in range(1, 1000):

     # change element target

     elem.click()

Update:

Using the advice given below, I was able to parse and interact with map elements by using the following approach:

l = driver.find_elements(By.XPATH,
    '//*[@id="map"]//descendant::*[name()="svg"]//descendant::*[@class="waypoints leaflet-interactive"]')

for i in l:
    ActionChains(driver).click(i).perform()

Solution

I think that I understood you. You want to do the curiosity’s route.
I found the xpath that you need //*[@id="map"]//descendant::*[name()="svg"]//descendant::*[name()="path"]
This xpath gives you all elements of the route, later with the method find_elements and the sentence for you could get all points of the curiosity’s route.

l = driver.find_elements(......)
#iterate through list
for i in l:
  # to do things

Answered By – javier Piña

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published