2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

はじめに

普段Seleniumを使っていて、ChromeDeveloperToolsに出てくるConsoleログを取得したいと思ったことが何度かあります。
Puppeteer等を使い取得する手段もありますが、Selenium4でも取得できるようになりそうなので、試してみた内容を書き留めます。
まだ、調べ途中なのと、Selenium4がα版で変更される可能性も高いので(1)としておきます。

環境情報

対象 Version
OS Mac OS X 10.15.5
java 1.8.0_252
Chrome 83.0.4103.116

ソースコード

build.gradle

今回は以下のライブラリを使用しました。

build.gradle

dependencies {
    //junit
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.6.2'
    testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.6.2'
    //selenium
    testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '4.0.0-alpha-6'
    //WebdriverManager
    testCompile group: 'io.github.bonigarcia', name: 'webdrivermanager', version: '4.0.0'
}

実装コード


//import文は省略

public class Selenium4Cdp {

  private ChromeDriver driver;

  @BeforeEach
  void setup(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }


  @Test
  void SeleniumSample() throws InterruptedException {

    DevTools devTools = driver.getDevTools();
    devTools.createSession();
    devTools.send(Log.enable());
    devTools.addListener(Log.entryAdded(), (responseReceived -> {
      String level = responseReceived.getLevel().toString();
      String text = responseReceived.getText();
      Optional<String> url = responseReceived.getUrl();
      System.out.println("[Log] " + level + ": " +  text + ": " +  url );
    }));

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

    System.out.println("========== HC Top ==========");
    driver.get("https://www.humancrest.co.jp");
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".btn.fade01")));

    System.out.println("========== HC Service ==========");
    driver.findElement(By.xpath("//a[text()='サービス ']")).click();
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[text()='テスト / 運用自動化サービス']")));

  }

  @AfterEach
  void tearDown(){
    driver.quit();
  }

}

実行結果

このような実装をすると、以下のようにLogLevelがErrorとWarningのものが出力されます。
devToolsの宣言をしてるのは1箇所ですが、画面遷移した時も常にConsoleログを取得してくれそうです。

========== Top ==========
[Log] error: Failed to load resource: the server responded with a status of 404 (): Optional[https://www.googletagmanager.com/gtm.js?id=XXXXXX]
[Log] warning: A cookie associated with a resource at http://lmsg.jp/ was set with `SameSite=None` but without `Secure`. A future release of Chrome will only deliver cookies marked `SameSite=None` if they are also marked `Secure`. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/XXXXXXX.: Optional[https://www.humancrest.co.jp/]
========== Service ==========
[Log] error: Failed to load resource: the server responded with a status of 404 (): Optional[https://www.googletagmanager.com/gtm.js?id=XXXXXX]

おわりに

実際にSeleniumのソースコードを見てみると、lineNumberやstack等、その他にも取得できるものが色々ありそうです。
Selenium4でできることについて分かったことが増えたら、また色々書こうと思います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?