0

I have more or less this structure, How can I select the next element after title? The starting point must be x or y since the structure have duplicated classes and so on, and this one is an unique anchor. Just to clarify I need to catch the content and the reference is the title.

x = wd.find_elements_by_class_name('title')[0] // print title 0
y = wd.find_elements_by_class_name('title')[1] // print title 1

HTML:

<div class='global'>
    <div class="main">
        <p class="title">Title0</p>
        <p class="content">Content0</p>
    </div>

    <div class="main">
        <p class="title">Title1</p>
        <p class="content">Content1</p>
    </div>
</div>
AMC
  • 2,535
  • 7
  • 12
  • 34
anvd
  • 3,953
  • 16
  • 60
  • 119
  • 1
    Does this answer your question? [Find next sibling element in Selenium, Python?](https://stackoverflow.com/questions/23887592/find-next-sibling-element-in-selenium-python) – AMC Feb 11 '20 at 16:04
  • https://stackoverflow.com/questions/2092303/xpath-for-choosing-next-sibling, https://stackoverflow.com/questions/49054985/python-selenium-getting-the-element-after-an-element?noredirect=1&lq=1 – AMC Feb 11 '20 at 16:05

2 Answers2

1

If you are using selenium try following css selector to get the p tag based on class title.

driver.find_elements_by_css_selector(".title+p")

To get the content value.

for item in driver.find_elements_by_css_selector(".title+p"):
    print(item.text)
KunduK
  • 29,126
  • 4
  • 11
  • 33
1

For Specific Element:

driver.find_element(By.XPATH, '(//p[@class()="title"]/following-sibling::p)[1]'  #Content0

driver.find_element(By.XPATH, '(//p[@class()="title"]/following-sibling::p)[2]'  #Content1

For all Elements:

for content in driver.find_elements(By.XPATH, '//p[@class()="title"]/following-sibling::p'):
    print(content.text)
Muzzamil
  • 2,665
  • 2
  • 8
  • 22