'How to find specific element in list and its' button (Selenium Python)

I am trying to find an specific element with class='name' <h3Brook/h3> then click on the specific button inside of that div. Only care about "Brook" which is under one of the 'puppy_list'

def find_web_element(self, by_locator, element_text):
    web_element=WebDriverWait(self.driver, 10).until(EC.visibility_of_element_located(by_locator))
    if web_element == name:
        return web_element
    else:
        pass

Then I want to find the button inside that element I found (brook) that's under class="rounded_button" type="submit" value="View Details" in that same div class with name Brook.

I'm able to do it by hardcoding the xpath but trying to find it by name:

self.homePage=HomePage(self.driver)
    brook = (By.XPATH, '/html/body/div/div[1]/div[3]/div[2]/div/div[4]/form/div/input') #Hardcoded path for Brook
    self.homePage.click_details(brook)
    self.DetailsPage=DetailsPage(self.homePage.driver)

Where click is just click by_locator

ADDING HTML CODE:

  <div class="puppy_list">
    <div class="list_line_odd">
      <div class="image"><img alt="Brook" class="list_image" src="/assets/Brook-ed6c0be3a8830921c5a954d1bc283354.jpg"></div>
      <div class="name">
        <h3>Brook</h3>
      </div>
      <div class="details">
        <h4>Golden Retriever</h4>
        <h4>Female</h4>
      </div>
      <div class="view">
        <form action="/puppies/4" class="button_to" method="get">
          <div><input class="rounded_button" type="submit" value="View Details"></div>
        </form>
      </div>
    </div>
  </div>
  <div class="puppy_list">
    <div class="list_line_even">
      <div class="image"><img alt="Hannah" class="list_image" src="/assets/Hannah-8f4449876737fadb369a7c7bab7fb0da.jpg"></div>
      <div class="name">
        <h3>Hanna2</h3>
      </div>
      <div class="details">
        <h4>Labrador Retriever Mix</h4>
        <h4>Female</h4>
      </div>
      <div class="view">
        <form action="/puppies/3" class="button_to" method="get">
          <div><input class="rounded_button" type="submit" value="View Details"></div>
        </form>
      </div>
    </div>
  </div>
  <div class="puppy_list">
    <div class="list_line_odd">
      <div class="image"><img alt="Maggiemae" class="list_image" src="/assets/MaggieMae-20224817b655d9dc556645ecf49c8d60.jpg"></div>
      <div class="name">
        <h3>Maggie Mae</h3>
      </div>
      <div class="details">
        <h4>Border Colie Mix</h4>
        <h4>Female</h4>
      </div>
      <div class="view">
        <form action="/puppies/1" class="button_to" method="get">
          <div><input class="rounded_button" type="submit" value="View Details"></div>
        </form>
      </div>
    </div>
  </div>
  <div class="puppy_list">
    <div class="list_line_even">
      <div class="image"><img alt="Ruby_sue" class="list_image" src="/assets/ruby_sue-6a9a75d0fc2d367acca04c13390c3f7a.jpg"></div>
      <div class="name">
        <h3>Ruby Sue</h3>
      </div>
      <div class="details">
        <h4>Pit Bull Terrier</h4>
        <h4>Female</h4>
      </div>
      <div class="view">
        <form action="/puppies/8" class="button_to" method="get">
          <div><input class="rounded_button" type="submit" value="View Details"></div>
        </form>
      </div>
    </div>
  </div>
  <br clear="all">
  <p></p>
  <div class="pagination"><span class="previous_page disabled">← Previous</span> <em class="current">1</em> <a rel="next" href="/agency/index?page=2">2</a> <a href="/agency/index?page=3">3</a> <a class="next_page" rel="next" href="/agency/index?page=2">Next →</a></div>
  <p></p>

</div>


Solution 1:[1]

You can find the info on 'Brook' based on the h3 tag

brook = driver.find_element_by_xpath("//h3[(text()= 'Brook')]")

Which will output <h3>Brook</h3>. You can get to the parent div by using xpath /... The parent div is <div class="name"><h3>Brook</h3></div>, so you want to move to the grandparent div by using /.. again.

brook = driver.find_element_by_xpath("//h3[(text()= 'Brook')]/../..")

Then, select your button by

click_button = brook.find_element_by_xpath("//input[@class='rounded_button']")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mitchell Olislagers