所以在Chrome 63中,现在支持多客户端远程调试(https://developers.google.com/web/updates/2017/10/devtools-release-notes)
我想要实现的是将Chrome DevTools Protocol HeapProfiler与一些硒测试一起使用。我正在运行版本 64 Chrome 开发频道和 ChromeDriver 2.33。
ChromeOptions options = newChromeOptions();
options.addArguments("--remote-debugging-port=9222");
WebDriver driver = new ChromeDriver(options);
... selenium stuff
一个新的镶边窗口将打开并挂起,直到超时。我可以通过帮助>谷歌浏览器检查版本来确认打开的chrome窗口是chrome 64。我收到此错误,似乎是网络驱动程序失去连接。
Exception in thread "main" org.openqa.selenium.WebDriverException: chrome not
reachable
DevTools 协议正在工作,因为我能够在另一个 chrome 窗口中打开 http://localhost:9222 并查看调试界面。
有没有人能够让这两件事一起工作?
谢谢:)
这里的问题是,如果您通过"远程调试端口"开关,那么chromedriver就会有一个错误,它仍然在内部分配一个randon端口并继续尝试连接到它而不是连接到9222端口。
options.addArguments("--remote-debugging-port=9222");
我们可以通过跳过此命令开关并让 chrome 决定此随机端口并从 chromedriver 日志中提取此端口号来解决此问题。
我让它工作,在这里我详细地写了它。
https://medium.com/@sahajamit/selenium-chrome-dev-tools-makes-a-perfect-browser-automation-recipe-c35c7f6a2360
Selenium 4版本将具有用于Chrome DevTools协议的用户友好API。我刚刚完成了Selenium Java客户端的网络和性能域的实现。https://github.com/SeleniumHQ/selenium/pull/7212
此外,Java 客户端中还有一个通用 API,该 API 是不久前合并的。所有这些新功能可能会在下一个 Alpha 版本中发布。
这是一篇关于如何使用日志的好文章:https://codoid.com/selenium-4-chrome-devtools-log-entry-listeners/
以下是我从远程调试中获取所需信息的方法,此外,还可以防止定义端口。我通过SeleniumLog-API获得它
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.setBinary(chromeBin);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
LoggingPreferences logPref = new LoggingPreferences();
logPref.enable(LogType.DRIVER, Level.ALL);
driverInstance = new ChromeDriver(capabilities);
LogEntries x = driverInstance.manage().logs().get(LogType.DRIVER);
for(LogEntry e:x.getAll()){
if(e.getMessage().contains("DevTools request:")){
String url = e.getMessage().replaceFirst("DevTools request:", "").trim();
}
if(e.getMessage().contains("DevTools response:")){
String json = e.getMessage().replaceFirst("DevTools response:", "");
try {
if("page".equals(JSONUtil.get(json,"type" ))){
webSocketDebuggerUrl = JSONUtil.get(json,"webSocketDebuggerUrl" );
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
System.out.println(e.getMessage());
}
我使用的 JSONUtil 是我自己的工具,所以不要怀疑,只需替换为从 jsontext 中提取的任何代码即可。
这是一个相当健壮的Java实现,使用与Selenium 3.13和cdp4j 3.0.2-SNAPSHOT相同的目标选项卡。轻松翻译成任何语言。
package com.company;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import io.webfolder.cdp.session.SessionFactory;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;
public class Main {
public static void main(String[] args) {
System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, "C:\path\to\chromedriver.exe");
var driver = new ChromeDriver();
try {
var cdp = findCdpEndpoint(driver);
System.out.println(cdp.toString());
try (var factory = new SessionFactory(cdp.getPort())) {
driver.navigate().to("https://google.com");
String seTargetId = getSeTargetId(cdp, driver.getTitle());
try (var session = factory.connect(seTargetId)) {
session.waitDocumentReady();
session.sendKeys("Astronauts");
driver.getKeyboard().sendKeys(Keys.RETURN);
session.wait(2000);
driver.navigate().to("http://www.google.com");
session.waitDocumentReady();
}
}
} catch (Exception ex) {
System.out.println(ex.toString());
}
driver.quit();
}
private static String getSeTargetId(URL cdp, String title) throws IOException {
for (JsonElement element : new JsonParser().parse(new InputStreamReader(cdp.openStream(), "UTF-8")).getAsJsonArray()) {
var object = element.getAsJsonObject();
if (title == null || title.isEmpty()
? object.get("type").getAsString().equalsIgnoreCase("page")
: object.get("title").getAsString().equalsIgnoreCase(title)) {
return object.get("id").getAsString();
}
}
throw new IllegalStateException("Selenium target not found.");
}
private static URL findCdpEndpoint(WebDriver driver) throws IOException {
var capChrome = (Map<?,?>) ((HasCapabilities)driver).getCapabilities().getCapability("chrome");
var userDataDir = (String) capChrome.get("userDataDir");
var port = Integer.parseInt(Files.readAllLines(Paths.get(userDataDir, "DevToolsActivePort")).get(0));
return new URL("http", "localhost", port, "/json");
}
}