1

I have learned a similar question, but the answer does not fit my work.

I am working at getting redirectChain by selenium performance log after clicking on all the ad links in an original page.And I found out that if the link opens in orginal tab, then the performance log can gather all the traffic information, from which I can further extract redirectResponse url.

However, to make sure that the original page won't change when clicking on a link, I tried to send ctrl+click command to show new page in another tab. And now, I can only get traffic information in the original page.

Are there any solutions on my problem? Can I change some params of performance log so that I can get all the webs open by a driver in another tabs?

For reference, here is my testing code:

import json
import pprint
import time
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
#import action chains
from selenium.webdriver.common.action_chains import ActionChains
#import Keys
from selenium.webdriver.common.keys import Keys

capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {"performance": "ALL"}
options = webdriver.ChromeOptions()
options.add_argument('user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36')
driver = webdriver.Chrome(
    r"chromedriver.exe",
    desired_capabilities=capabilities,
    options = options,
)
script = "Object.defineProperties(navigator, {webdriver:{get:()=>undefined}})"
driver.execute_script(script)

def process_browser_logs_for_network_events(logs):
    for entry in logs:
        log = json.loads(entry["message"])["message"]
        if (
                "Network.response" in log["method"]
                or "Network.request" in log["method"]
                or "Network.webSocket" in log["method"]
        ):
            yield log

driver.get("https://stackoverflow.com/questions/35592602/how-can-i-get-a-intermediate-url-from-a-redirect-chain-from-selenium-using-pytho")

element = driver.find_element_by_xpath("//a[@class = 'ws-nowrap s-btn s-btn__primary']")
action = ActionChains(driver)
action.move_to_element(element).key_down(Keys.CONTROL)\
    .click(element).key_up(Keys.CONTROL).perform()
#action.move_to_element(element).click(element).perform()
#print(driver.current_url)

windows = driver.window_handles

driver.switch_to_window(windows[-1])
time.sleep(5)
logs = driver.get_log("performance")
events = process_browser_logs_for_network_events(logs)
with open("log_entries1.txt", "wt") as out:
    for event in events:
        pprint.pprint(event, stream=out)
hans
  • 325
  • 2
  • 9

0 Answers0