我正在嘗試從以下站點中提取多種型別。(我已經知道網址) https://www.discogs.com/master/1515454-Zedd-Katy-Perry-365
<div class="profile">
<h1 id="profile_title" class="hide_mobile has_action)menu">...<h1>
<div class="head">Genre:<div> ==$0
<div class="content">
<a href="/genre/electronic">Electronic</a>
", "
<a href="/genre/pop">Pop</a>
這是我的 Python 代碼
genre = None
try:
genre = driver.find_element_by_xpath("[contains(concat(' ', @class, ' '), ' profile ')]//*[contains(@href, ' /genre/* '").text
如何將流派提取到文本中?(例如電子、流行音樂)
uj5u.com熱心網友回復:
要在網站中提取和列印Genre ie Electronic、Pop等的值,您需要為visibility_of_all_elements_located()誘導WebDriverWait并且您可以使用以下任一定位器策略:
使用XPATH:
driver.get("https://www.discogs.com/master/1515454-Zedd-Katy-Perry-365") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click() print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//tr/th[@scope='row' and contains(., 'Genre')]//following::td[1]//a")))])控制臺輸出:
['Electronic', 'Pop']注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/459110.html
上一篇:無法定位硒中flex下存在的元素
下一篇:如何使硒中只有一個值?
