0

I'm trying to get all the "title" of the class "lien-jv topic-title", my code is running without any error. But the print function is showing nothing, the code just ends without showing any result.

What I want to get is all the topics title in the page. In the example below, it would be: "Pourquoi les gens déprécient le COSTARD/COSTUME de nos jours ?"

<a class="lien-jv topic-title" href="/forums/42-51-68065824-1-0-1-0-pourquoi-les-gens-deprecient-le-costard-costume-de-nos-jours.htm" title="Pourquoi les gens déprécient le COSTARD/COSTUME de nos jours ?">Pourquoi les gens déprécient le COSTARD/COSTUME de nos jours ?
                            </a>

Website = https://www.jeuxvideo.com/forums/0-51-0-1-0-1-0-blabla-18-25-ans.htm

import os
import webbrowser  
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.by import By
import time

os.system('cls')

browser = webdriver.Chrome(executable_path=r"C:\Users\Desktop\Webdriver\chromedriver.exe")
browser.get('https://www.jeuxvideo.com/forums/0-51-0-1-0-1-0-blabla-18-25-ans.htm')
time.sleep(2)

list_topic_main=browser.find_elements_by_css_selector("lien-jv topic-title")
for x in list_topic_main:
    print(x.title)

browser.close()

os.system('pause')

Thanks !

Meriole
  • 127
  • 8

3 Answers3

0

Use class name not css selector:

list_topic_main = browser.find_elements_by_class_name("lien-jv topic-title")

And i believe you meant text not title:

print(x.text)
Pedro Maia
  • 2,666
  • 1
  • 5
  • 20
0

There's a accept cookie pop up, that you need to click first.

Also I am using below CSS and then looking for title attribute using .get_attribute()

Code :

browser = webdriver.Chrome(executable_path=r"C:\Users\Desktop\Webdriver\chromedriver.exe")
browser .maximize_window()
#browser .implicitly_wait(30)
wait = WebDriverWait(browser, 30)

browser .get("https://www.jeuxvideo.com/forums/0-51-0-1-0-1-0-blabla-18-25-ans.htm")
try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[onclick='Didomi.setUserAgreeToAll();']"))).click()
except:
    pass

for title in driver.find_elements(By.CSS_SELECTOR, "a.lien-jv.topic-title"):
    print(title.get_attribute('title'))

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Output :

Règles du forum.
QUAND avez vous ENTENDU parler du GRAND REMPLACEMENT pour la PREMIÈRE FOIS
"Je vais PARTIR au CANADA / DANEMARK / NORVÈGE" :rire:
A quoi jouer après The Last of Us 2 ?
DAVOS prévoit un BLACKOUT MONDIAL en OCTOBRE
Je sors avec une 9/10 et vous ?
Zevent 2021 | 29, 30, 31 Octobre 2021
Pascal OP : je m'en fous de l'avis de ma famille je suis toujours
[OFFICIEL] No Nut November 2021
ALLER sur VINTED, voir un CUL moulé DANS un JEAN..
PASCAL OP se BRANLE pendant qu'il CONDUIT sur l'autoroute
[VIDEO] le CAS des MEUFS qui S'EMBROUILLENT pour RIEN ( CAS D'ETUDES )
CNEWS, Renaud camus sur le GR : « J’encourage les Français à croire ce qu’ils voient »
Mort du père du Tiramisu
C'est quoi la LOGIQUE du CHANGEMENT D'HEURE ?
Je reprends contact avec une pute du lycée sur DISCORD
[FIC] "Tu l'as encore jamais fait Célestin ?? "
Asselineau : "Madame gros caca xDDD"
CÉLESTIN à Hoywood bordel
Notez moi MECHAMMENT /10
Ahii quel mot est né la même que VOUS ?
(FILLE) Ce 6/10 PETIT de TAILLE qui fait de la DRAGUE de RUE
"Le rap cette sous culture pour débile"
ma lapine s’ENDORT aux chiottes
DAVOS prévoit un BLACKOUT MONDIAL en OCTOBRE
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
0

To print the value of the title attribute i.e. Pourquoi les gens déprécient le COSTARD/COSTUME de nos jours ? you can use either of the following Locator Strategies:

  • Using css_selector:

    print(driver.find_element(By.CSS_SELECTOR, "a.lien-jv.topic-title[href*='pourquoi-les-gens-deprecient']").get_attribute("value"))
    
  • Using xpath:

    print(driver.find_element(By.XPATH, "//a[@class='lien-jv topic-title' and contains(@href, 'pourquoi-les-gens-deprecient')]").get_attribute("value"))
    

Ideally you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "a.lien-jv.topic-title[href*='pourquoi-les-gens-deprecient']"))).get_attribute("value"))
    
  • Using XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//a[@class='lien-jv topic-title' and contains(@href, 'pourquoi-les-gens-deprecient')]"))).get_attribute("value"))
    
  • 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
    

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python


References

Link to useful documentation:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352