0

My scenario is: I opened a webpage (it has different links) and clicked on a link randomly. Now I want to get the URL of that webpage opened.

I searched a lot and found the same answer everywhere which I tried.

driver.get(url)
driver.current_url

It every time returns me the URL of the main webpage, not that which is opened by clicking a link randomly. What I have observed is, such is happening because on clicking link new webpage opens up in a new tab and current_url returns me URL of the page that is on the first tab. Any solution to this problem?

Vinay M S
  • 25
  • 5
Maha Waqar
  • 585
  • 1
  • 10
  • 24
  • Take a look to [switch to new window](https://stackoverflow.com/questions/10629815/how-to-switch-to-new-window-in-selenium-for-python) and [how to switch to the new browser window which opens after click on the button](https://stackoverflow.com/questions/9588827/how-to-switch-to-the-new-browser-window-which-opens-after-click-on-the-button) – Sers Aug 22 '19 at 09:42

2 Answers2

1

In order to work with new tab you have to switch driver to it.

You can use Selenium methods to check current window and move to another one:

driver.window_handles

to find a list of window handles and after try to switch using following methods.

- driver.switch_to.active_element      
- driver.switch_to.default_content
- driver.switch_to.window
splekhanov
  • 116
  • 1
  • 2
  • 9
0

I would say you need to gather URL from link itself. I mean "href" attribute. If there is just some onClick event and URL is generated dynamically you need to adjust this function to pass url where you need it.