Home > Mobile >  Selenium (python) getting distinct div texts nested within list items
Selenium (python) getting distinct div texts nested within list items

Time:01-28

Have the following HTML from which I need to extract the text of three variables (descr_confezione, aic_farmaco and stato_confezione) for each of the four list items:

<ul id="ul_lista_confezioni">
  <li style="display: list-item;">
    <div >" 100 MG COMPRESSE RIVESTITE " 100 COMPRESSE</div>
    <div >036655037</div>
    <div >revocato</div>
  </li>
  <li style="display: list-item;">
    <div >" 100 MG COMPRESSE RIVESTITE " 50 COMPRESSE</div>
    <div >036655025</div>
    <div >revocato</div>
  </li>
  <li style="display: list-item;">
    <div >" 50 MG COMPRESSE RIVESTITE " 100 COMPRESSE</div>
    <div >036655013</div>
    <div >revocato</div>
  </li>
  <li style="display: list-item;">
    <div >" 50 MG COMPRESSE RIVESTITE " 50 COMPRESSE</div>
    <div >036655049</div>
    <div >revocato</div>
  </li>
</ul>

My current code:

DRIVER_WAIT.until(
  EC.visibility_of_element_located(
    (By.XPATH, '//*[@id="ul_lista_confezioni"]/li')
  )
)
for pp in driver.find_elements(By.XPATH, '//*[@id="ul_lista_confezioni"]/li'):
   print(pp.text)

returns the three desired values but joined in a single string:

" 100 MG COMPRESSE RIVESTITE " 100 COMPRESSE036655037revocato

If I expand the XPath to also include the nested div as in:

'//*[@id="ul_lista_confezioni"]/li/div'

then

print(pp.text)

only returns the first value I need such as:

" 100 MG COMPRESSE RIVESTITE " 100 COMPRESSE

CodePudding user response:

ul_Element = driver.find_element_by_id("ul_lista_confezioni")
li_ElementList = ul_Element.find_elements_by_tag_name("li")

for li in li_ElementList:
    descr_confezione_TEXT = li.find_element_by_class_name("descr_confezione").text
    aic_farmaco_TEXT = li.find_element_by_class_name("aic_farmaco").text
    stato_confezione_TEXT = li.find_element_by_class_name("stato_confezione").text

    print(descr_confezione_TEXT)
    print(aic_farmaco_TEXT)
    print(stato_confezione_TEXT)

CodePudding user response:

driver.get('url here')
time.sleep(3) # you may use webdriverwait instead of time.sleep for effectiveness
eles = driver.find_elements(By.XPATH, "//*[@id='ul_lista_confezioni']/li//div")
x = [el.text for el in eles]
print(x)

Output:

['" 100 MG COMPRESSE RIVESTITE " 100 COMPRESSE', '036655037', 'revocato', '" 100 MG COMPRESSE RIVESTITE " 50 COMPRESSE', '036655025', 'revocato', '" 50 MG COMPRESSE RIVESTITE " 100 COMPRESSE', '036655013', 'revocato', '" 50 MG COMPRESSE RIVESTITE " 50 COMPRESSE', '036655049', 'revocato']

CodePudding user response:

You are using the correct locator, i.e. fine_elements, this Xpath should give you what you need: //div[contains(@class, 'descr_')]

  •  Tags:  
  • Related