1

I am trying to get the complete URL from address bar of chrome. I am using this code.

import re
from selenium import webdriver

driver = webdriver.Chrome(r'C:\Users\XYZ\Desktop\ABC\chromedriver_win32\chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
#time.sleep(1) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('India Gate')
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
currentURL = driver.current_url
print(currentURL)
time.sleep(5)
driver.quit()

Instead of getting a complete URL in the address bar I am only getting https://google.com/maps

I need a URL like this https://www.google.com/maps/place/India+Gate/@28.6337379,77.2060128,15z/data=!4m5!3m4!1s0x390ce2daa9eb4d0b:0x717971125923e5d!8m2!3d28.612912!4d77.2295097

What am I missing? Please help. It's my first time using Selenium.

2
  • 1
    What happens if you add time.sleep(5) right after click on search button? Commented Jan 7, 2020 at 8:31
  • Right, I did that and now I am getting the complete location Commented Jan 7, 2020 at 8:49

3 Answers 3

2

To extract the url you need to induce WebDriverWait for the url_contains() and you can use the following solution:

  • Code Block:

    import re
    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    driver = webdriver.Chrome(r'C:\Users\XYZ\Desktop\ABC\chromedriver_win32\chromedriver.exe')  # Optional argument, if not specified will search path.
    driver.get('https://www.google.com/maps');
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("India Gate")
    driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
    WebDriverWait(driver, 20).until(EC.url_contains("Gate"))
    print(driver.current_url)
    driver.quit()
    
  • Console Output:

    https://www.google.com/maps/place/India+Gate/@28.6129167,77.227321,17z/data=!3m1!4b1!4m5!3m4!1s0x390ce2daa9eb4d0b:0x717971125923e5d!8m2!3d28.612912!4d77.2295097
    

Update

As an alternative instead of url_contains("Gate") you can use url_contains("data=") as follows:

WebDriverWait(driver, 10).until(EC.url_contains("data="))
Sign up to request clarification or add additional context in comments.

6 Comments

hi, i am still getting the same response
@AMal Checkout the updated answer and let me know the status.
same response. I am using Jupyter. Do you think that might affect the output?
I tried changing the code to directly save the URL in a text file. There too I am only getting the shortened response and the not complete URL like you are getting at your console output.
@AMal Shouldn't have made any difference within Jupyter and save the URL in a text file as well.
|
1

The same code block provided by DebanjanB worked when I increased the wait time.

import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(r'C:\Users\XYZ\Desktop\ABC\chromedriver_win32\chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.NAME, "q"))).send_keys("India Gate")
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
WebDriverWait(driver, 30).until(EC.url_contains("Gate"))
print(driver.current_url)
driver.quit()

This may be because your internet connection is slow or other factors.

1 Comment

It's still not working for me. Let me try in Firefox. Maybe it's a browser issue.
0

According your feedback on my comment I would suggest you the following code snippet:

import re
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome(r'C:\Users\XYZ\Desktop\ABC\chromedriver_win32\chromedriver.exe')  # Optional argument, if not specified will search path.
driver.get('https://www.google.com/maps');
#time.sleep(1) # Let the user actually see something!
search_box = driver.find_element_by_name('q')
search_box.send_keys('India Gate')
driver.find_element_by_css_selector("button[id='searchbox-searchbutton']").click()
WebDriverWait(driver, 10).until(EC.url_contains("https://www.google.com/maps/place/")) # here you are waiting until url will match your output pattern
currentURL = driver.current_url
print(currentURL)
time.sleep(5)
driver.quit()

PS avoid as much as possible hardcoded pause

3 Comments

I remember you have provided great answers earlier implementing WebDriverWait :) why to fall back to sleep () again?
Once you are a master, there shouldn't be any downgradation ;)
Why time.sleep(5) before driver.quit() ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.