我正在使用 selenium 来拦截网络请求,现在我编写了一个监听并拦截请求的代码,但我正在努力将所有请求写入 json 文件,这是我的代码:
public class SeleniumFourFeaturesTest {
WebDriver driver;
DevTools tool;
@BeforeClass
public void startSession() {
//Set Chrome Driver
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
//Set Dev-Tools and create a session
tool = ((ChromeDriver)driver).getDevTools();
tool.createSession();
}
@Test(enabled=false)
public void test1() {
driver.findElement(By.xpath("//div[@class='list-group']/a[1]")).click();
driver.findElement(By.id("ap_email")).sendKeys("test123");
driver.findElement(By.id("ap_password")).sendKeys("12345");
driver.findElement(RelativeLocator.withTagName("input").below(By.id("ap_password"))).click();
}
@Test
public void interceptRequestAndContinue() throws InterruptedException {
//enable Network
tool.send(Network.enable(Optional.empty(), Optional.empty(), Optional.empty()));
//add listener to intercept request and continue
tool.addListener(Network.requestIntercepted(),
requestIntercepted -> tool.send(
Network.continueInterceptedRequest(requestIntercepted.getInterceptionId(),
Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty(),
Optional.empty(),
Optional.empty(), Optional.empty())));
//set request interception only for css requests
RequestPattern requestPattern = new RequestPattern("*.css", ResourceType.Stylesheet, InterceptionStage.HeadersReceived);
tool.send(Network.setRequestInterception(ImmutableList.of(requestPattern)));
driver.get("https://www.google.com");
Thread.sleep(30000);
}
}
我希望在代码的末尾能够将请求写入 json 文件