LoginSignup
3
2

More than 1 year has passed since last update.

WebDriver からのレスポンスを、curl コマンドで確認する

Last updated at Posted at 2022-03-01

注意!記事中に「Chronium」という表記が出てきますが、正しくは「Chromium」です!
自身の戒めのためにこのまま残しますが、閲覧する方はお間違いないよう、よろしくお願い致します!

curl から WebDriver にリクエスト投げる情報は数あれど・・・

Chrome と Edge の WebDriver へのリクエストとそれに対するレスポンスの違いを見るために、Windows 上で curl コマンドを使ってみた
自分の覚書が主目的ですが、Chrome と Edge の違いをメインに記載

準備

WebDriver を起ち上げておきます

curl コマンド実行

セッションIDの取得

① 新しいセッションを起ち上げる(Chrome、Edge(Chroniumモード))
curl -sSL -X POST -H "Content-Type: application/json" -d "{ ""capabilities"" : {} }" localhost:9515/session
①のレスポンス(Chrome)
{"value":
  {"capabilities":
    {"acceptInsecureCerts":false,
     "browserName":"chrome",
     "browserVersion":"98.0.4758.102",
     "chrome":
       {"chromedriverVersion":"98.0.4758.102 (273bf7ac8c909cde36982d27f66f3c70846a3718-refs/branch-heads/4758@{#1151})",
        "userDataDir":"C:\\Users\\〇〇\\AppData\\Local\\Temp\\scoped_dir17272_1370734459"},
        "goog:chromeOptions":{"debuggerAddress":"localhost:63016"
       },
     "networkConnectionEnabled":false,
     "pageLoadStrategy":"normal",
     "platformName":"windows",
     "proxy":{},
     "setWindowRect":true,
     "strictFileInteractability":false,
     "timeouts":{"implicit":0,"pageLoad":300000,"script":30000},
     "unhandledPromptBehavior":"dismiss and notify",
     "webauthn:extension:credBlob":true,
     "webauthn:extension:largeBlob":true,
     "webauthn:virtualAuthenticators":true
    },
   "sessionId":"809b912ab018ef2954ac0fed769689ab"
  }
}
①のレスポンス(Edge(Chroniumモード))
{"value":
  {"capabilities":
    {"acceptInsecureCerts":false,
     "browserName":"msedge",
     "browserVersion":"98.0.1108.62",
     "ms:edgeOptions":{"debuggerAddress":"localhost:53176"},
     "msedge":
       {"msedgedriverVersion":"98.0.1108.62 (86b4ba0c0a320a2c0c88adba983ad3b5ce15710f)",
        "userDataDir":"C:\\Users\\〇〇\\AppData\\Local\\Temp\\scoped_dir19736_1150601222"
       },
     "networkConnectionEnabled":false,
     "pageLoadStrategy":"normal",
     "platformName":"windows",
     "proxy":{},"setWindowRect":true,
     "strictFileInteractability":false,
     "timeouts":{"implicit":0,"pageLoad":300000,"script":30000},
     "unhandledPromptBehavior":"dismiss and notify",
     "webauthn:extension:credBlob":true,
     "webauthn:extension:largeBlob":true,
     "webauthn:virtualAuthenticators":true
    },
   "sessionId":"bc00f7cc4a172ac84ea1c7b80196847a"
  }
}

② 新しいセッションを起ち上げる(Edge(Edgeモード))
curl -sSL -X POST -H "Content-Type: application/json" -d "{ ""desiredCapabilities"" : {}, ""requiredCapabilities"" : {} }" localhost:9515/session
②のレスポンス(Edge(Edgeモード))
{"sessionId":"c52cb7b6e21abd8a58d11c6869a1c943",
 "status":0,
 "value":
   {"acceptInsecureCerts":false,
    "acceptSslCerts":false,
    "browserConnectionEnabled":false,
    "browserName":"msedge",
    "cssSelectorsEnabled":true,
    "databaseEnabled":false,
    "handlesAlerts":true,
    "hasTouchScreen":false,
    "javascriptEnabled":true,
    "locationContextEnabled":true,
    "mobileEmulationEnabled":false,
    "ms:edgeOptions":{"debuggerAddress":"localhost:57832"},
    "msedge":
      {"msedgedriverVersion":"98.0.1108.62 (86b4ba0c0a320a2c0c88adba983ad3b5ce15710f)",
       "userDataDir":"C:\\Users\\〇〇\\AppData\\Local\\Temp\\scoped_dir10300_301355156"
      },
    "nativeEvents":true,
    "networkConnectionEnabled":false,
    "pageLoadStrategy":"normal",
    "platform":"Windows",
    "proxy":{},
    "rotatable":false,
    "setWindowRect":true,
    "strictFileInteractability":false,
    "takesHeapSnapshot":true,
    "takesScreenshot":true,
    "timeouts":{"implicit":0,"pageLoad":300000,"script":30000},
    "unexpectedAlertBehaviour":"ignore",
    "version":"98.0.1108.62",
    "webStorageEnabled":true,
    "webauthn:extension:credBlob":true,
    "webauthn:extension:largeBlob":true,
    "webauthn:virtualAuthenticators":true
  }
}

① の方法で Edge で新セッションを作ると、その後の振る舞いがほぼ Chrome と一致します
以降、便宜上①で立ち上げた Edge を Chroniumモード、②で立ち上げた Edge を Edge モードと記載します

注目すべきは、SessionIDの記載されている階層が違う
取得するときは気を付けましょう

その他

①と②でリクエストとレスポンスにほぼ違いが無い物

③ 指定のURLに遷移
curl -sSL -X POST -H "Content-Type: application/json" -d "{""url"": ""https://www.google.co.jp/""}" localhost:9515/session/取得したセッションID/url
③ のレスポンス(Edgeモードの場合)
{"sessionId":"リクエストしたセッションID","status":0,"value":null}
④ 指定elementをクリック
curl -sSL -X POST -H "Content-Type: application/json" -d "{}" localhost:9515/session/取得したセッションID/element/取得したエレメントID/click
④ のレスポンス(CromeおよびChroniumモードの場合)
{"value":null}

厳密にはレスポンスが少し違いますが、正常に実行された場合 value に null が返るのは共通です、value の階層も同じです
ちなみに、④ は Edge モードの場合は

④' 指定elementをクリック
curl -sSL -X POST localhost:9515/session/取得したセッションID/element/取得したエレメントID/click

の様に、リクエストヘッダやリクエストボディが無くてもレスポンスが得られますが、ChroniumモードのEdgeやChromeは、エラーとなります
Chrome や Chronium モードの Edge は、リクエストボディが必要無さそうなシチュエーションでも空のリクエストボディを送る必要がある事がほとんどのはず(詳しくは検証して無いけど、今まで検証した限りはそうでした)
Edgeモードでも④のリクエストで通るので、④の方法で問い合わせるのが共通で良いです

①と②でリクエストはほぼ同じでもレスポンスに違いが有る物

⑤ xpath 等を指定して elementid を取得するリクエスト
curl -sSL -X POST -H "Content-Type: application/json" -d "{""using"": ""xpath"", ""value"": ""//*[@id='gbwa']/div/a""}" localhost:9515/session/取得したセッションID/element
⑤ のレスポンス(ChromeおよびChroniumモードの場合)
{"value":{"element-6066-11e4-a52e-4f735466cecf":"取得したエレメントID"}}
⑤ のレスポンス(Edgeモードの場合)
{"sessionId":"リクエストしたセッションID","status":0,"value":{"ELEMENT":"取得したエレメントID"}}

まとめ

この辺の WebDriver の差異を吸収してるseleniumって、凄い!

3
2
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
3
2