Using beautiful soup in a new tab

Viewed 25

The code I am running opens up a webpage to get data from. Once I open up a new tab and try to scrape the data from that webpage it just scrapes it from the original webpage. Is there some sort of command or function I should try. The line to create a new tab -

driver.switch_to.new_window('tab')

any help would be greatly appreciated

1 Answers

First, get all the windows handles and switch to another window or tab using the below code:

windows = driver.window_handles
   for window in windows:
       driver.switch_to.window(window)

If you have more than 2 windows or tabs:

def switch_to_windows(self, title):
    windows = driver.window_handles
    for window in windows:
        driver.switch_to.window(window)
        print("Window title: ",driver.title, ", Id: ",window)
        if driver.title == title:
            break
    return True

pass the page's title to ensure you are on the right window or tab. Then you can scrape the data from the correct tab.

Related