0

I work with python 3.7 on windows and I want to write a script that print the actual active urls on browsers (chrome and firefox), I found the script: import webbrowser webbrowser.open(the url) but this script allows to open the url not to find the active urls. can someone help me

  • https://stackoverflow.com/questions/2764586/get-current-url-in-python/2764822 Does this answer your query ? – Echo Apr 19 '20 at 16:43

2 Answers2

1

Here you need to download and install pywin32 and import these modules in your script like this -

import win32gui
import win32con

#to get currently active windows
window = win32gui.GetForegroundWindow

Or to get the Google Chrome window handle

win32gui.FindWindow

Sai prateek
  • 11,842
  • 9
  • 51
  • 66
0

You can use selenium module and loop through all open tabs.

This code prints all open tabs of chrome and firefox:

from selenium import webdriver

chromeDriver = webdriver.Chrome()
firefoxDriver = webdriver.Firefox()


for handle in chromeDriver.window_handles[0]:
    chromeDriver.switch_to.window(handle)
    print(chromeDriver.current_url)

for handle in firefoxDriver.window_handles[0]:
    firefoxDriver.switch_to.window(handle)
    print(firefoxDriver.current_url)


Note:

This code is inefficient and should change to use only one loop.

shahar
  • 355
  • 2
  • 18