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 5 years have passed since last update.

xpathで指定の祖先を取得する

Posted at

「祖先」であっているのかどうか。。。

以下のようなHTMLがあったとして

target
<div class='row'>
    <div class='col-md-12'>
        <div class='form-group'>
            <label class='control-label col-md-4'>ID</label>
            <div class='col-md-8'>
                <input type='text' name='id' class='form-control input-sm'>
            </div>
        </div>
        <div class='form-group'>
            <label class='control-label col-md-4'>パスワード</label>
            <div class='col-md-8'>
                <input type='password' name='password' class='form-control input-sm'>
            </div>
        </div>
    </div>
</div>

IDのform-groupを取得したい場合は以下のように指定できる

xpath
//input[@name='id']/ancestor::div[2]

div[1] だとcol-md-8 が取得できる
指定した数字分親を遡る感じ

これでseleniumの自動化が捗る

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?