Aditya
Aditya

Reputation: 37

How to get the URL of the current webpage in python?

Im using selenium and Im trying to change the driver to a new page it opens (same tab) driver.switch_to doesnt seem to work because i think its used when a new window is opened driver.current_url also doesnt seem to work because its giving me the url of the previous page and i cant seem to figure out how to get the url of the current page

Heres the code :

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver.get("https://www.youtube.com")

searchBar = driver.find_element_by_name("search_query")
searchBar.send_keys("unbox therapy")
searchBar.send_keys(Keys.ENTER)

print(driver.current_url)

This still returns https://www.youtube.com i need the search query which should look something like https://www.youtube.com/results?search_query=unbox+therapy

Upvotes: 2

Views: 120

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193298

@TekNath answer was in the right direction and was near perfect. However I would suggest to avoid a time.sleep(5) in your code as:

time.sleep(secs) suspends the execution of the current thread for the given number of seconds. The argument may be a floating point number to indicate a more precise sleep time. The actual suspension time may be less than that requested because any caught signal will terminate the sleep() following execution of that signal’s catching routine. Also, the suspension time may be longer than requested by an arbitrary amount because of the scheduling of other activity in the system.

You can find a detailed discussion in How to sleep webdriver in python for milliseconds

As an alternative, you can induce WebDriverWait for title_contains() and you can use the following Locator Strategy:

  • 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
    from selenium.webdriver.common.keys import Keys
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_experimental_option("excludeSwitches", ["enable-automation"])
    options.add_experimental_option('useAutomationExtension', False)
    driver = webdriver.Chrome(options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.youtube.com/")
    searchBar = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#search")))
    searchBar.send_keys("unbox therapy")
    searchBar.send_keys(Keys.ENTER)
    WebDriverWait(driver, 10).until(EC.title_contains("unbox therapy"))
    print(driver.current_url)
    
  • Console Output:

    https://www.youtube.com/results?search_query=unbox+therapy
    

Upvotes: 0

Tek Nath
Tek Nath

Reputation: 1789

You should add some wait time before driver.current_url As it takes some time to fully load the site. And the load time also depends on internet connection speed and others factors. For me it worked without wait time.

import time
from selenium.webdriver.common.keys import Keys
from selenium import webdriver

driver = webdriver.Chrome("/home/teknath/Desktop/chromedriver")
driver.get("https://www.youtube.com")

searchBar = driver.find_element_by_name("search_query")
searchBar.send_keys("unbox therapy")
searchBar.send_keys(Keys.ENTER)
time.sleep(5)
print(driver.current_url)

Upvotes: 1

Noah
Noah

Reputation: 614

Like Tek Nath said, you could add wait time, but you could also do something like this.

searchTerm = "unbox therapy"
replaceSpaceWithPlus(searchTerm)
driver.get("https://www.youtube.com/results?search_query=" + searchTerm)

Upvotes: 0

Related Questions