LoginSignup
0

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の自動化が捗る

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
0