Home > OS >  Python. selenium. How to locate particular tags under a specific id
Python. selenium. How to locate particular tags under a specific id

Time:08-30

<div id="was-price">
   <div >
      <div ></div>
      <div >
          <span >$</span>
          <span>509</span>
          <span >40</span>
      </div>
   </div>

I am trying to locate all the span elements under the div with id=was-price. I tried this way. driver.find_element_by_id('was-price').find_element(By.TAG_NAME,'span'). and this did not work. Please advise, what is the right way to locate these span elements. Thanks

CodePudding user response:

This line

driver.find_element_by_id('was-price').find_element(By.TAG_NAME,'span')

should work if you want to locate single span node. If you want to get list try

driver.find_element_by_id('was-price').find_elements(By.TAG_NAME,'span')

You can also try XPath:

driver.find_elements_by_xpath('//div[@id="was-price"]//span')

CodePudding user response:

Additionally to mentioned by JaSON you can locate all these elements with single css_selector as following:

driver.find_elements_by_css_selector('div#was-price span')
  • Related