示例代碼
<a href="/cristiano/" title="cristiano">\<span class="\_7UhW9 xLCgt qyrsm KV-D4 se6yk T0kll "\>cristiano\</span\></a>
如何告訴 python 查找具有class、title、href和tabindex屬性的每個元素。然后獲取每個的“標題”值。
試過find_element_by_xpath但沒有結果
uj5u.com熱心網友回復:
語法是"//*[@attribute_name]"獲取
所有具有titleXPath 屬性的元素"//*[@title]",
獲取所有具有classXPath 屬性的元素"//*[@class]",獲取所有具有XPath 屬性
的元素
等。
因此,從所有元素中獲取所有屬性值在頁面上,您可以執行以下操作:href"//*[@href]"title
elements = driver.find_elements(By.XPATH, "//*[@title]")
for element in elements:
title = element.get_attribute("title")
print(title)
等等
如果你想找到所有具有title and class 和 href屬性的元素,每個元素中的所有 3 個,它看起來像這樣:
elements = driver.find_elements(By.XPATH, "//*[@title and @class and @href]")
for element in elements:
title_val = element.get_attribute("title")
class_val = element.get_attribute("class")
href_val = element.get_attribute("href")
print(title_val)
print(class_val)
print(href_val)
uj5u.com熱心網友回復:
要查找具有class、title、href和 textract 的每個元素,您可以使用串列title推導的每個專案的屬性值,您可以使用以下任一定位器策略:
使用xpath:
print([my_elem.get_attribute("title") for my_elem in driver.find_elements(By.XPATH, "//a[@href and @title][.//span[@class and text()]]")])誘導 WebDriverWait:
print([my_elem.get_attribute("title") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@href and @title][.//span[@class and text()]]")))])注意:您必須添加以下匯入:
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/caozuo/448267.html
