我想從網站上獲取所有屬性值名稱“href”,其中有 10 個。我使用以下方法成功獲得了一個:
url = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[2]/div[2]/div/div/div[2]/section[1]/div/div[2]/div[3]/ul/li[1]/div/div[1]/span/a"))).get_attribute("href")
這樣做的問題是它只回饋一個而不是全部。我試圖通過 ID 去,但它沒有回傳任何東西:
url = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "a"))).get_attribute("href")
此外,其他 href 值位于不同的 xpath 中:
/html/body/div[2]/div[2]/div/div/div[2]/section[1]/div/div[2]/div[3]/ul/li[2]/div/div[1]/span/a
/html/body/div[2]/div[2]/div/div/div[2]/section[1]/div/div[2]/div[3]/ul/li[3]/div/div[1]/span/a
/html/body/div[2]/div[2]/div/div/div[2]/section[1]/div/div[2]/div[3]/ul/li[4]/div/div[1]/span/a
這是我的元素:
<a ph-tevent="job_click" ref="linkEle" href.bind="getUrl(linkEle, 'job', eachJob, '', eachJob.jobUrl)" data-ph-at-id="job-link" data-ph-id="ph-page-element-page20-CRUCUZ" class="au-target" au-target-id="181" ph-click-ctx="job" ph-tref="12313123213" ph-tag="ph-search-results-v2" href="https://hyperlink.com" data-ph-at-job-title-text="title" data-ph-at-job-location-text="Unknown" data-ph-at-job-location-area-text="asd" data-ph-at-job-category-text="Manufacturing" data-access-list-item="2" data-ph-at-job-id-text="A123124" data-ph-at-job-type-text="Regular" data-ph-at-job-industry-text="Manufacturing" data-ph-at-job-post-date-text="2021-12-09T00:00:00.000Z" data-ph-at-job-seqno-text="ASD212ASFS" aria-label="Senior Manager">
<div class="job-title" data-ph-id="ph-page-element-page20-0Mi3Ce">
<!--anchor-->
<!--anchor-->
<span data-ph-id="ph-page-element-page20-PLxqta">Senior Manager </span>
</div><!--anchor--> </a>
任何幫助表示贊賞!
uj5u.com熱心網友回復:
要查找多個 Web 元素,您應該使用find_elements,或者如果您正在使用顯式等待,那么您可以使用presence_of_all_elements_locatedor visibility_of_all_elements_located。
根據您共享的 HTML,如果
這個CSS
a[ph-tevent='job_click'][ref='linkEle']
或者這個xpath
//a[@ph-tevent='job_click' and @ref='linkEle']
代表所有節點,檢查以下是步驟:
如果我們有所有需要的節點條目,請檢查dev tools(谷歌瀏覽器)。HTML DOM
檢查步驟:
Press F12 in Chrome-> 轉到element部分 -> 執行CTRL F-> 然后粘貼xpath并查看,如果您想要elements的是否突出顯示。
如果是,那么下面的代碼應該可以作業:
for ele in driver.find_elements(By.XPATH, "//a[@ph-tevent='job_click' and @ref='linkEle']"):
print(ele.get_attribute('href'))
uj5u.com熱心網友回復:
使用Selenium提取href屬性的值和Python您必須為visibility_of_all_elements_located()誘導WebDriverWait并且您可以使用以下任一Locator Strategies:
使用CSS_SELECTOR:
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a[ph-tevent='job_click'][ref='linkEle'][data-ph-at-id='job-link'][ph-click-ctx='job'][href]")))])使用XPATH:
print([my_elem.get_attribute("href") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@ph-tevent='job_click' and @ref='linkEle'][@data-ph-at-id='job-link' and @ph-click-ctx='job'][@href]")))])注意:您必須添加以下匯入:
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/qianduan/411730.html
標籤:
