phast
phast

Reputation: 212

Getting current url after selenium navigation?

I have a Python script that runs Selenium and makes a search for me on YouTube. After my .send_keys() and .submit() commands I attempt to get the current url of the search page with print(driver.current_url) but it only gives me the original url from my driver.get('https://www.youtube.com') command. How can I get the full current url path of the search page once I'm there? For example https://www.youtube.com/results?search_query=election instead of https://www.youtube.com. Thank you.

Upvotes: 1

Views: 2163

Answers (4)

Jaspreet Kaur
Jaspreet Kaur

Reputation: 127

To get the current URL after clicking on random videos on particular search, current_url is the only way.

The reason because of which you are getting the previous URL may be the page is not loaded, you may check for the page load by comparing the title of the page

For eg:

expectedURL = "demo class"
actualURL=driver.title
assert expectedURL == actualURL

If the assert gives you true then you may have the command to get the current URL

driver.current_url

Upvotes: 0

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

You can simply wait for a period of time.

driver.implicitly_wait(5)
print(driver.current_url)

Upvotes: 1

Justin Lambert
Justin Lambert

Reputation: 978

Why are you practicing social media to automation For multiple reasons, logging into sites like Gmail and Facebook using WebDriver is not recommended. Aside from being against the usage terms for these sites (where you risk having the account shut down), it is slow and unreliable.

The ideal practice is to use the APIs that email providers offer, or in the case of Facebook the developer tools service which exposes an API for creating test accounts, friends, and so forth. Although using an API might seem like a bit of extra hard work, you will be paid back in speed, reliability, and stability. The API is also unlikely to change, whereas webpages and HTML locators change often and require you to update your test framework.

Logging in to third-party sites using WebDriver at any point of your test increases the risk of your test failing because it makes your test longer. A general rule of thumb is that longer tests are more fragile and unreliable.

WebDriver implementations that are W3C conformant also annotate the navigator object with a WebDriver property so that Denial of Service attacks can be mitigated.

Upvotes: 0

rahul rai
rahul rai

Reputation: 2326

As you have not shared the code you have tried. I am guessing issue is with your page load. After clicking on submit you are not giving any time for page to load before you get your url. Please give some wait time. The simplest ( No so good) way is to use :

time.sleep(5)
print(driver.current_url)

Above will wait for 5 sec.

Upvotes: 1

Related Questions