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

Seleniumにて要素の隣の要素を取得したい(Python)

Posted at

<div>
    <span id="target">Target Element</span>
    <span id="sibling1">Sibling 1</span>
    <span id="sibling2">Sibling 2</span>
</div>
# targetの後に続く兄弟要素のリストを取得できる。
siblings = driver.find_elements(By.XPATH, "//span[@id='target']/following-sibling::span")
# targetの前の続く兄弟要素のリストを取得できる。
siblings = driver.find_elements(By.XPATH, "//span[@id='target']/preceding-sibling::span")
# CSS_SELECTORでも指定できる(知らなかった...)
siblings = driver.find_elements(By.CSS_SELECTOR, "#target ~ span")

HTMLやjsができる人からすると常識かもしれませんが、pythonだけやっているとこんな入り組んだことができるとは想像もつきませんね...

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