1
0

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 1 year has passed since last update.

Selenium Webdriver での Role 指定によるパーツの取得方法について

Posted at

はじめに

以下の理由から ARIA 属性をロケーターに使うことがより良いとされています

  • ソフトウェアの使用方法にテストが沿っていると信頼性が高まる
  • 自然と正しくroleやラベルを設定する力学もはたらく = アクセシブルになる

結論

Selenium Webdriver では他のテストツールのように簡潔に実装はできない、かつ取れない要素もある。

以下、他のツールの実装例。

ex. Playwright get_by_role

page.get_by_role("button", name="ボタンです2")
<div role="button">
	<p>ボタンです1</p>
</div>
<button>
	<p>ボタンです2</p>
</button>

いい感じに「ボタンです2」の要素が指定できる。(動作未確認)

Selenium Webdriver で TRY

Ruby の Selenium Webdriver ライブラリのfind_element メソッドでは以下の種類の探索方法をサポートしている。

{
  class: 'class name',
  class_name: 'class name',
  css: 'css selector',
  id: 'id',
  link: 'link text',
  link_text: 'link text',
  name: 'name',
  partial_link_text: 'partial link text',
  relative: 'relative',
  tag_name: 'tag name',
  xpath: 'xpath'
}.freeze

Role はサポートされていないので、上記のいずれかで表現する必要がある。

以下のHTML を xpath で探索してみる。

<div role="button">
	<p>ボタンです1</p>
</div>
<button>
	<p>ボタンです2</p>
</button>
  • //*[@role="button"]
    • 「ボタンです1」上のdiv のみ探索される
    • 明示的にrole を定義していないと探索されない
  • //*[@role="button"]//*[text()="ボタンです1"]
    • 「ボタンです1」の p タグが探索される
    • 指定のロールのタグの下のテキストノード( ARIA の Name )に当たるものを擬似的に探索することはできる

結論

Playwright などのテスティングフレームワークほど簡潔に実装はできないかつ、取れない要素もありました :cry:

他にやり方をご存じの方いましたらコメントください。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?