Search code examples
pythonpandasselenium-webdriverdownload

How to get names of person from webpage in selenium


From Python I have opened the following webpage:

https://www.interpol.int/en/How-we-work/Notices/View-Red-Notices

chromedriver_path = r"./driver/chromedriver"
browser = webdriver.Chrome(executable_path=chromedriver_path)
url = "https://www.interpol.int/en/How-we-work/Notices/View-Red-Notices"


topics_xpath = '//*[@id="noticesResultsItemList"]/div[1]/div/div/div[2]/div[1]/a'
browser.get(url)
escolhe = browser.find_element("xpath", topics_xpath)
time.sleep(10)
escolhe.click()
time.sleep(60)
browser.close()

Now, I need to get the names of the people shown in that page.

Does anybody know how to do it, please?


Solution

  • Selenium has a very useful implicit wait feature which means that use of sleep() should be redundant. Use of this feature means that Selenium will wait for up to the specified number of seconds for an element to come into existence.

    Something like this:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    
    IW = 5
    URL = 'https://www.interpol.int/en/How-we-work/Notices/View-Red-Notices'
    
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless")
    
    service = Service('/Volumes/G-Drive/chromedriver')
    
    with webdriver.Chrome(service=service, options=chrome_options) as wbe:
        wbe.implicitly_wait(IW)
        wbe.get(URL)
        if (div := wbe.find_element(By.ID, 'noticesResultsItemList')):
            for sd in div.find_elements(By.CLASS_NAME, 'redNoticeItem__labelLink'):
                print(sd.text.replace('\n', ' '))
    

    Output:

    PRZYSIUDA MARIUSZ
    RIBEIRO ROSANGELA APARECIDA
    GOROSITO PEREZ DANIEL PATRICIO
    RIFFI RABIE
    ESPINOZA CONDORI EDWIN ALFREDO
    PINTO GONZÁLEZ TULIO DE JESUS
    ZEAS MENDOZA JOSE DAVID
    HERNANDEZ CASTRO JOSE IVAN
    DE CARVALHO MELO QUIRINO DE MORAIS ELSON
    SINGH VIKRAMJEET
    CARDONA CABRERA MISRAEL
    ABREGO FUENTES JOSE ARMANDO
    CORTEZ LOPEZ VICTOR MANUEL
    ANGEL FLORES MELVIN JOSSUE
    GUEVARA PAZ JAIME AMILCAR
    ESCOBAR SÁNCHEZ OSCAR ANTONIO
    MENDOZA BELTRAN JOSE ABDON
    IBARRA MARTINEZ AURELIO
    BOUGUETTAIA ABDELKADER
    KOSZCZYK RYSZARD