Issue
Hi I am going to save google map traffic every 5 min on Sunday. So I am using typical traffic part and selenium to reach to the webpage
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('https://www.google.com/maps/@48.1152117,-1.6634771,12.79z/data=!5m1!1e1')
elem1 = driver.find_element_by_xpath('//*[@id=":2"]')
elem1.click()
elem2 = driver.find_element_by_xpath('//*[@id=":1"]/div')
elem2.click()
Sunday = driver.find_element_by_xpath('//*[@id="layer"]/div/div/div/div/div[1]/div[1]/button[1]')
Sunday.click()
The problem is changing the time. I do not know how to do that.
Solution
This should work:
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
# Load the page
driver = webdriver.Firefox()
driver.get('https://www.google.com/maps/@48.1152117,-1.6634771,12.79z/data=!5m1!1e1')
# Wait and click the menu button
wait = WebDriverWait(driver, 10)
elem1 = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[contains(@class, "goog-menu-button-caption")]')))
elem1.click()
# Change the option
actions = ActionChains(driver)
actions.send_keys(Keys.DOWN).send_keys(Keys.ENTER)
actions.perform()
# Sunday selector
Sunday = driver.find_element_by_xpath('//*[@id="layer"]/div/div/div/div/div[1]/div[1]/button[1]')
Sunday.click()
# Perform hour change
hour = driver.find_element_by_xpath('//div[contains(@class,"widget-layer-slider-range")]//span[contains(@class, "widget-layer-slider-thumb")]')
hour.click()
for i in range(1, 10):
time.sleep(1)
actions = ActionChains(driver)
actions.send_keys(Keys.RIGHT)
actions.perform()
After the page is loaded you need to wait for the menu button to be clickable (there are some ajax requests processed in the background).
To change the option I intended to use a regular click, but it did not do any change in the UI and also Selenium did not throw any exception. It is working using an action chains and keyboard commands.
Then is the selector for the day, you may change this to your needs.
To change the hour I also used action chains with keyboard commands.
Answered By - nezhar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.