Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
88 views
in Technique[技术] by (71.8m points)

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

Website architecture

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

Please help

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>
question from:https://stackoverflow.com/questions/65918681/how-to-find-specific-element-in-list-and-its-button-selenium-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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']")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...