我想title通過 selenium 從中獲得價值,但我無法做到這一點。需要專家意見。

items = driver.find_elements_by_tag_name("li")
for item in items:
followers = link.get_attribute('title')
print (followers)
它向我展示了一個空字串。為什么會這樣?
uj5u.com熱心網友回復:
如果下面的xpath
//li//a[@href='/cristiano/followers']
在 中是唯一的HTML-DOM,那么您可以使用以下代碼:
wait = WebDriverWait(driver, 20)
try:
a_tag = wait.until(EC.visibility_of_element_located((By.XPATH, "//li//a[@href='/cristiano/followers']")))
print(a_tag.find_element(By.XPATH, ".//descendant::span").get_attribute('title'))
except:
print("something went wrong")
pass
對于此用例,您實際上并不需要回圈。
進口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
檢查元素是否唯一的步驟:
Press F12 in Chrome-> 轉到element部分 -> 執行CTRL F-> 然后粘貼xpath并查看,如果您想要element的是否使用匹配節點突出顯示。1/1
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/474369.html
標籤:Python html python-3.x 硒
