KHADIJA KOSSENTINI
KHADIJA KOSSENTINI

Reputation: 11

get active url on chrome and firefox using python

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

Upvotes: 0

Views: 2394

Answers (2)

shahar
shahar

Reputation: 365

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.

Upvotes: 0

Sai prateek
Sai prateek

Reputation: 11896

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

Upvotes: 1

Related Questions