1

How to get the updated url from firefox browser using selenium and Python? The below code is a very good working example of what I am trying to do. The script opens up a url, looks for the search bar in the webpage, pastes a particular product and then executes the search.

I am trying to extract the updated url after the search is completed which should be https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml but I am getting https://www.myntra.com/. How can I get the required url?

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

firefoxOptions = webdriver.FirefoxOptions()
firefoxOptions.set_preference("dom.webnotifications.enabled", False)

driver = webdriver.Firefox(firefox_options = firefoxOptions)

driver.implicitly_wait(5)

# Maximize the browser window
driver.maximize_window()

# navigate to the home page
driver.get("https://www.myntra.com/")

# Locate the text field to update values
text_field = driver.find_element_by_class_name("desktop-searchBar")

# Clears any value already present in text field
text_field.clear()

# Updates the string in search bar
text_field.send_keys("Avene Unisex Thermal Spring Water 50 ml")
text_field.send_keys(Keys.ENTER)

new_page = driver.current_url
print(new_page)

driver.close()

2 Answers 2

1

Seems you were pretty close. You need to induce WebDriverWait for the url to get changed and you can use the following solution:

  • Code Block:

    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.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
    driver.get("https://www.myntra.com/")
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.desktop-searchBar"))).send_keys("Avene Unisex Thermal Spring Water 50 ml")
    driver.find_element_by_css_selector("a.desktop-submit").click()
    WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.title-title")))
    print(driver.current_url)
    driver.quit()
    
  • Console Output:

    https://www.myntra.com/avene-unisex-thermal-spring-water-50-ml
    
Sign up to request clarification or add additional context in comments.

5 Comments

Just curious to know why WebDriverWait has been used before the driver.find_element line.
That was my bad the extra line. Fixed it. Let me know if you have a query now.
This worked. Thank you. Quite honestly, I am not sure what the WebDriverWait steps are doing and why it has been used twice. I still have a lot to learn.
also, In the above example, is there any way to make the webdriver click on the product that was searched? I would appreciate it if you could guide me on this when you have the time.
Glad that you have raised the question. The first WebDriverWait() is to wait (initial page loading/html rendering) for the first desired element i.e. the search box to be clickable so you can send the text. The second WebDriverWait() is to wait (new page/html rendering) for the next desired element i.e the <h1> element to be visible which will ensure the url have settled, so we can retrieve the url for the next steps/validation. Let me know if any doubts.
0

You need to wait after text_field.send_keys(Keys.ENTER) to get information updated, if you don't want waits then try to use click() method instead of send_keys

Comments

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.