メインコンテンツにスキップ

switchFrame

アクティブなコンテキストを、ページ上の iframe などのフレームに切り替えます。ページ上のフレームをクエリする方法は複数あります。

  • 文字列が与えられた場合、その文字列に一致するコンテキスト ID、URL、またはその文字列を含む URLを持つフレームに切り替えます。

    // switch to a frame that has a specific url or contains a string in the url
    await browser.url('https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_iframe')
    // Note: this frame is located in a nested iframe, however you only need to provide
    // the frame url of your desired frame
    await browser.switchFrame('https://www.w3schools.com')
    // check the title of the page
    console.log(await browser.execute(() => [document.title, document.URL]))
    // outputs: [ 'W3Schools Online Web Tutorials', 'https://www.w3schools.com/' ]
  • フレームのコンテキスト ID がある場合は、それを直接使用できます。

    // switch to a frame that has a certain context id
    await browser.switchFrame('A5734774C41F8C91D483BDD4022B2EF3')
  • WebdriverIO の要素で、iframe 要素を参照しているものが与えられた場合、そのフレームに切り替えます。

    // switch to a frame element queried from current context
    await browser.switchFrame($('iframe'))
  • 関数が与えられた場合、ページ上のすべての iframe をループ処理し、コンテキストオブジェクト内で関数を呼び出します。関数は、フレームを選択する必要があるかどうかを示すブール値を返す必要があります。関数はブラウザ内で実行され、すべての Web API (例:

    // switch to first frame that contains an element with id "#frameContent"
    await browser.switchFrame(() => Boolean(document.querySelector('#frameContent')))
    // switch to first frame that contains "webdriver" in the URL
    await browser.switchFrame(() => document.URL.includes('webdriver'))
  • null が与えられた場合、最上位のフレームに切り替えます。

    // first switch into a frame
    await browser.switchFrame($('iframe'))
    // do more automation within that frame, then ...

    // switch to the top level frame
    await browser.switchFrame(null)

フレームに切り替えると、それ以降のすべてのコマンドは、別のページへの移動を含め、そのフレームのコンテキストで実行されます。

使い方
browser.switchFrame(context)
パラメータ
名前詳細
contextstring, object, function

ようこそ!何かお手伝いできることはありますか?

WebdriverIO AI Copilot