0

I would like to loop over the elements from this website https://www.dccomics.com/comics

At the bottom of the webpage there is a browse comics section, I would like to scrape the names from each comic

This is the code I have at the moment

# imports
from selenium import webdriver
from bs4 import BeautifulSoup 
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By


# website urls
base_url = "https://www.dccomics.com/"
comics_url = "https://www.dccomics.com/comics"

# Chrome session
driver = webdriver.Chrome("C:\\laragon\\www\\Proftaak\\chromedriver.exe")
driver.get(comics_url)
driver.implicitly_wait(500)


cookies = driver.find_element_by_xpath('/html/body/div[1]/div[2]/div[4]/div[2]/div/button')
driver.execute_script("arguments[0].click();", cookies)
driver.implicitly_wait(100)
clear_filter = driver.find_element_by_class_name('clear-all-action')
driver.execute_script("arguments[0].click();", clear_filter)

array = []
for titles in driver.find_elements_by_class_name('result-title'):
title = titles.find_element_by_xpath('/html/body/div[2]/section/section/div[2]/div/div/div/div/div[3]/div[7]/div[2]/div/div/div/div/div[3]/div[3]/div[2]/div[1]/a/p[1]').text


    array.append({'title': title,})
    print(array)
driver.quit()

I am using the xpath below:

/html/body/div[2]/section/section/div[2]/div/div/div/div/div[3]/div[7]/div[2]/div/div/div/div/div[3]/div[3]/div[2]/div[1]/a/p[1] 

This works but only gets the first element of the result-title CSS class, in this case 818.

How would I loop through each result-title class using either the CSS selector or Xpath?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
niels van hoof
  • 469
  • 4
  • 19

1 Answers1

1

To scrape the names from each comic using Selenium and you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://www.dccomics.com/comics')
    print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.browse-result>a p:not(.result-date)")))])
    
  • Using XPATH:

    driver.get('https://www.dccomics.com/comics')
    print([my_elem.text for my_elem in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[contains(@class, 'browse-result')]/a//p[not(contains(@class, 'result-date'))]")))])
    
  • Console Output:

    ['PRIMER', 'DOOMSDAY CLOCK PART 2', 'CATWOMAN #22', 'YOU BROUGHT ME THE OCEAN', 'ACTION COMICS #1022', 'BATMAN/SUPERMAN #9', 'BATMAN: GOTHAM NIGHTS #7', 'BATMAN: THE ADVENTURES CONTINUE #5', 'BIRDS OF PREY #1', 'CATWOMAN 80TH ANNIVERSARY 100-PAGE SUPER SPECTACULAR #1', 'DC GOES TO WAR', "DCEASED: HOPE AT WORLD'S END #2", 'DETECTIVE COMICS #1022', 'FAR SECTOR #6', "HARLEY QUINN: MAKE 'EM LAUGH #1", 'HOUSE OF WHISPERS #21', 'JOHN CONSTANTINE: HELLBLAZER #6', 'JUSTICE LEAGUE DARK #22', 'MARTIAN MANHUNTER: IDENTITY', 'SCOOBY-DOO, WHERE ARE YOU? #104', 'SHAZAM! #12', 'TEEN TITANS GO! TO CAMP #15', 'THE JOKER: 80 YEARS OF THE CLOWN PRINCE OF CRIME THE DELUXE EDITION', 'THE LAST GOD: TALES FROM THE BOOK OF AGES #1', 'THE TERRIFICS VOL. 3: THE GOD GAME']
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352