実現したいこと
あるクラスの中に4つの「pタグ」があり、さらにその中に4つの「aタグ」があります。(下図)
私は取得したいタグは下記の2つです。
・1番目の「pタグ」の中の2番目の「aタグ」
・3番めの「pタグ」の中の4番目の「aタグ」
これらを単発で指定する方法が知りたいです。
試したこと
子孫要素を指定する
tag = driver.find_elements_by_css_selector("div a")
同じ階層の何番目かを指定できる
tag = driver.find_enelemts_by_css_selector("a:nth-of-type(番号)")
などを組合せたりしたのですが、解決に至りませんでした。
HTML
1<div class=‘addcontents’> 2 <p style=“text-align: left;”> 3 <a style=‘長い文字列’></a> 4 <a style=‘長い文字列’></a> #指定したいもの 5 <a style=‘長い文字列’></a> 6 <a style=‘長い文字列’></a> 7 </p> 8 9 <p style=“text-align: left;”> 10 <a style=‘長い文字列’></a> 11 <a style=‘長い文字列’></a> 12 <a style=‘長い文字列’></a> 13 <a style=‘長い文字列’></a> 14 </p> 15 16 <p style=“text-align: left;”> 17 <a style=‘長い文字列’></a> 18 <a style=‘長い文字列’></a> 19 <a style=‘長い文字列’></a> 20 <a style=‘長い文字列’></a> #指定したいもの 21 </p> 22 23 <p style=“text-align: left;”> 24 <a style=‘長い文字列’></a> 25 <a style=‘長い文字列’></a> 26 <a style=‘長い文字列’></a> 27 <a style=‘長い文字列’></a> 28 </p>

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/11/16 15:10