How to get redirect chain with selenium if I must directly click on banners and open it on another tab? How to get redirect chain with selenium if I must directly click on banners and open it on another tab? selenium selenium

How to get redirect chain with selenium if I must directly click on banners and open it on another tab?


In order to get the redirect chains, you'll need the HAR files.

There are a few packages that combine selenium with other libraries to accomplish this (and other additions as well).

One is browsermob-proxy.

BrowserMob Proxy allows you to manipulate HTTP requests and responses, capture HTTP content, and export performance data as a HAR file. BMP works well as a standalone proxy server, but it is especially useful when embedded in Selenium tests.

Here is an example:

from browsermobproxy import Serverserver = Server("path/to/browsermob-proxy")server.start()proxy = server.create_proxy()from selenium import webdriverchrome_options = webdriver.ChromeOptions()chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) #Configure chrome optionsdriver = webdriver.Chrome(chrome_options=chrome_options)proxy.new_har("StackOverFlow") driver.get("https://stackoverflow.com")print(proxy.har)

There are other libraries such as selenium-wire that have similar capabilities (with other features as well).

Note: no need to open the Network panel.

Make sure to download the proxy and add the path to the initiation of the Server.