3

I want to get the current url when I am running Selenium. I looked at this stackoverflow page: How do I get current URL in Selenium Webdriver 2 Python? and tried the things posted but it's not working. I am attaching my code below:

from selenium import webdriver
#launch firefox
driver = webdriver.Firefox()
url1='https://poshmark.com/search?'

# search in a window a window
driver.get(url1)
xpath='//input[@id="user-search-box"]'
searchBox=driver.find_element_by_xpath(xpath)

brand="freepeople"

style="top"

searchBox.send_keys(' '.join([brand,"sequin",style]))

from selenium.webdriver.common.keys import Keys
#EQUIValent of hitting enter key
searchBox.send_keys(Keys.ENTER)



print(driver.current_url)

my code prints https://poshmark.com/search? but it should print: https://poshmark.com/search?query=freepeople+sequin+top&type=listings&department=Women because that is what selenium goes to.

Bob
  • 279
  • 6
  • 13
  • The issue is that there is no lag between your `searchBox.send_keys(Keys.ENTER)` and `print(driver.current_url)`. There should be some time lag, so that the statement can pick the url change. If your code fires before url has actually changed, it gives you old url only. The workaround would be to add `time.sleep(1)` to wait for 1 second. – Tarun Lalwani Mar 30 '18 at 20:04
  • huh I was wondering if that was it – Bob Mar 30 '18 at 20:19
  • that was the issue! thank you! :) – Bob Mar 30 '18 at 20:20

3 Answers3

4

The issue is that there is no lag between your searchBox.send_keys(Keys.ENTER) and print(driver.current_url).

There should be some time lag, so that the statement can pick the url change. If your code fires before url has actually changed, it gives you old url only.

The workaround would be to add time.sleep(1) to wait for 1 second. A hard coded sleep is not a good option though. You should do one of the following

  • Keep polling url and wait for the change to happen or the url
  • Wait for a object that you know would appear when the new page comes
  • Instead of using Keys.Enter simulate the operation using a .click() on search button if it is available

Usually when you use click method in selenium it takes cared of the page changes, so you don't see such issues. Here you press a key using selenium, which doesn't do any kind of waiting for page load. That is why you see the issue in the first place

Tarun Lalwani
  • 142,312
  • 9
  • 204
  • 265
3

I had the same issue and I came up with solution that uses default explicit wait (see how explicit wait works in documentation).

Here is my solution

class UrlHasChanged:
def __init__(self, old_url):
    self.old_url = old_url

def __call__(self, driver):
    return driver.current_url != self.old_url:


@contextmanager
def url_change(driver):
     current_url = driver.current_url
     yield
     WebDriverWait(driver, 10).until(UrlHasChanged(current_url))

Explanation:

  1. At first, I created my own wait condition (see here) that takes old_url as a parameter (url from before action was made) and checks whether old url is the same like current_url after some action. It returns false when both urls are the same and true otherwise.
  2. Then, I created context manager to wrap action that I wanted to make, and I saved url before action was made, and after that I used WebDriverWait with created before wait condition.

Thanks to that solution I can now reuse this function with any action that changes url to wait for the change like that:

    with url_change(driver):
        login_panel.login_user(normal_user['username'], new_password)

    assert driver.current_url == dashboard.url

It is safe because WebDriverWait(driver, 10).until(UrlHasChanged(current_url)) waits until current url will change and after 10 seconds it will stop waiting by throwing an exception.

What do you think about this?

Oskar
  • 41
  • 4
0

I fixed this problem by clicking on the button by using href. Then do driver.get(hreflink). Click() was not working for me!