0

How to click on "get routes" on "https://maps.mapmyindia.com/direction" by selenium using Python? Thanks for your help!

What I tried? I followed this "python selenium click on button", but this does not click.

from selenium import webdriver
#from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
#from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Firefox(executable_path=r'C:\Users\User\Desktop\pyCode\geckodriver-v0.21.0-win64\geckodriver.exe')
driver.get("https://maps.mapmyindia.com/direction")
startLocation = driver.find_element_by_id("auto_start")
startLocation.send_keys("28.4592,77.0727")
endLocation = driver.find_element_by_id("auto_end")
endLocation.send_keys("28.4590,77.0725")

driver.find_element_by_css_selector('div.col-xs-6.pull-right.text-right').click()
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
aLearner
  • 7
  • 5

2 Answers2

0

I reviewed the page and it seems the "get routes" button has an id associated to it. You can simply use that

So the last line in your code should be:

driver.find_element_by_id("get_d").click()

You can use other selectors as well:

xpath: //a[text()='Get routes']
css: #get_d

When writing your test scripts, you can always validate selectors in the browser before including them in your test script. Below are a few simple approaches I follow to validate selectors:

  1. When using 'id', simply use the following javascript in your browser console: document.getElementById("get_d"). This should return an element in the browser console if the id you are using is valid.
  2. When using 'xpath', use the following line: $x("//a[text()='Get routes']") in the browser console. This will also return all elements associated with the xpath you mention
  3. When using 'css selector', use the following line: $$("#get_d"). Similar to xpath approach, this will return all elements associated with the css selector you mention
BountyHunter
  • 1,413
  • 21
  • 35
  • Thanks @BountyHunter! It worked. I have one more question. Can you pls tell me how to get the source code of this open page? – aLearner Sep 05 '18 at 10:46
  • simply use `driver.page_source` – BountyHunter Sep 05 '18 at 11:05
  • was following this https://stackoverflow.com/questions/44668998/selenium-how-to-get-page-source-code-after-clicking-a-button. but did not work. getting an error: "ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine" – aLearner Sep 05 '18 at 12:09
0

To click on the element with text as GET ROUTES on https://maps.mapmyindia.com/direction you need to:

  • Induce WebDriverWait for the element container to be present within the HTML DOM.
  • Then you need to remove the attribute style="display: none;"
  • Finally, you can send the character sequence and invoke click() method on the element with text as GET ROUTES.
  • Code Block:

    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
    
    driver=webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("https://maps.mapmyindia.com/direction")
    search_tab = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "div.tab-pane.fade.in.search-tab.active")))
    driver.execute_script("arguments[0].removeAttribute('style')", search_tab)
    driver.find_element_by_css_selector("input.form-control.as-input#auto").send_keys("28.4592,77.0727")
    driver.find_element_by_css_selector("input.form-control.as-input#auto_end").send_keys("28.4590,77.0725")
    driver.find_element_by_css_selector("h2.get-btn>a.get-route#get_d").click()
    
  • Browser Snapshot:

map_my_india

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352