Get all opened websites from Chrome in Python Get all opened websites from Chrome in Python selenium selenium

Get all opened websites from Chrome in Python


You can use this methodology to store the tab data and manipulate it:

windows = driver.window_handles

You can store the windows using the above method.

current_window = driver.current_window_handle

This method will give you the current window that is being handled. You can go through the list 'windows' and check if it is current_window to navigate between the tabs.

driver.switch_to.window(windows[5])

This method will switch to a desired tab but I assume you already have it.

Now how do you store the time spent after the tabs are opened?There are two ways to do it:

  1. Internally, by referring to a pandas dataframe or list
  2. Reading and writing to a file.

First you need to import the 'time' library inside the script

current_time=time.time()

current_time is an int representation of the current time. It's a linux timestamp.

In either one of these scenarios, you will need a structure such as this:

data=[]for i in range(0,len(windows)):    data.append([  windows[i] , time.time() ])

This will give a structure as below:

    [[window[0],1234564879],    [window[1],1234567896],...]

Here's the thing you miss:

for i in range(0,len(data)):   if time.time()-data[i][1] > 600  # If new timestamp minus the old one is bigger than 600 seconds       driver.switch_to(data[i][0])       driver.close()

My personal advice is that you start with stable API services to get whatever data you want instead of selenium. I would recommend SerpApi since I work there. It has variety of scrapers including a google results scraper, and it has 5000 free calls for new accounts.