Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用selenium性能日志在新选项卡中打开所有站点的流量?_Python_Selenium_Selenium Webdriver_Logging_Automated Tests - Fatal编程技术网

Python 如何使用selenium性能日志在新选项卡中打开所有站点的流量?

Python 如何使用selenium性能日志在新选项卡中打开所有站点的流量?,python,selenium,selenium-webdriver,logging,automated-tests,Python,Selenium,Selenium Webdriver,Logging,Automated Tests,我也学过类似的方法,但答案不适合我的工作 单击原始页面中的所有广告链接后,我正在通过selenium性能日志获取重定向链。我发现,如果链接在原始选项卡中打开,那么性能日志可以收集所有流量信息,我可以从中进一步提取重定向响应url 但是,为了确保在单击链接时原始页面不会更改,我尝试发送ctrl+click命令以在另一个选项卡中显示新页面。现在,我只能在原始页面中获取交通信息 我的问题有什么解决办法吗?我是否可以更改性能日志的一些参数,以便让驱动程序在其他选项卡中打开所有站点 以下是我的测试代码供参

我也学过类似的方法,但答案不适合我的工作

单击原始页面中的所有广告链接后,我正在通过selenium性能日志获取
重定向链
。我发现,如果链接在原始选项卡中打开,那么性能日志可以收集所有流量信息,我可以从中进一步提取重定向响应url

但是,为了确保在单击链接时原始页面不会更改,我尝试发送
ctrl+click
命令以在另一个选项卡中显示新页面。现在,我只能在原始页面中获取交通信息

我的问题有什么解决办法吗?我是否可以更改性能日志的一些参数,以便让驱动程序在其他选项卡中打开所有站点

以下是我的测试代码供参考:

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)