Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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

以下のような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の自動化が捗る

lyrical_erica
最近はphp、前はJavaをメインに書いてます。 あとCentOS7もいじってます。 趣味はpython。
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away