我正在嘗試獲取“lien-jv topic-title”類的所有“標題”,我的代碼運行時沒有任何錯誤。但是列印函式什么也沒顯示,代碼就結束了,沒有顯示任何結果。
我想得到的是頁面中的所有主題標題。在下面的示例中,它將是:“ 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>
網站 = 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')
謝謝 !
uj5u.com熱心網友回復:
class name不使用css selector:
list_topic_main = browser.find_elements_by_class_name("lien-jv topic-title")
我相信你的意思text不是title:
print(x.text)
uj5u.com熱心網友回復:
有一個接受 cookie 彈出,你需要先點擊。
我也在下面使用CSS,然后使用.get_attribute()
代碼 :
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'))
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
輸出 :
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
uj5u.com熱心網友回復:
要列印title屬性的值,即Pourquoi les gens déprécient le COSTARD/COSTUME de nos jours ?您可以使用以下任一定位器策略:
使用
css_selector:print(driver.find_element(By.CSS_SELECTOR, "a.lien-jv.topic-title[href*='pourquoi-les-gens-deprecient']").get_attribute("value"))使用
xpath:print(driver.find_element(By.XPATH, "//a[@class='lien-jv topic-title' and contains(@href, 'pourquoi-les-gens-deprecient')]").get_attribute("value"))
理想情況下,你需要引起WebDriverWait的visibility_of_element_located(),你可以使用以下的定位策略:
使用
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"))使用
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"))注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
您可以在如何使用 Selenium 檢索 WebElement 的文本 - Python 中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/345987.html
