Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get text of list inside h3 tag using selnium

import urllib3
import certifi
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import requests
from bs4 import BeautifulSoup
import time
import ssl
http = urllib3.PoolManager(ca_certs=certifi.where())
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(options=chrome_options, executable_path="D:\\python works\\driver\\chromedriver.exe")
URL= "https://physicians.wustl.edu/people/christopher-d-abraham-md/"
driver.get(URL)
time.sleep(10)
a = driver.find_element_by_xpath("//div[@class='wuphys-ppl affiliations']").text
print(a)
time.sleep(10)

HTML

<div class="wuphys-ppl affiliations">
  <h3>Hospital Affiliations</h3>
  <ul><li>Barnes-Jewish Hospital</li>
  <li>Barnes-Jewish West County Hospital</li>
  <li>Christian Hospital - Northeast</li></ul>
</div>

**Required_output:**
Barnes-Jewish Hospital
Barnes-Jewish West County Hospital
Christian Hospital - Northeast

The output should be Barnes-Jewish Hospital, Barnes-Jewish West County Hospital, Christian Hospital – Northeast. How to get the list from h3 tag using selnium

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your xpath should look like this, to get only the texts from the <li> and not from the <h3>:

//div[@class='wuphys-ppl affiliations']/ul//li

To get all texts you have to use find_elements_by_xpath() ant iterat over ResultSet:

a = driver.find_elements_by_xpath("//div[@class='wuphys-ppl affiliations']/ul//li")
for e in a:
    print(e.text)

Output:

Barnes-Jewish Hospital
Barnes-Jewish West County Hospital
Christian Hospital - Northeast
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading