簡體   English   中英

SELENIUM(Python):如何在單擊后檢索元素將我重定向到(打開新選項卡)的 URL? 元素有<a>標簽但沒有 href</a>

[英]SELENIUM (Python) : How to retrieve the URL to which an element redirects me to (opens a new tab) after clicking? Element has <a> tag but no href

我正在嘗試抓取一個包含產品列表的網站,如果單擊該網站,則將用戶重定向到包含更多信息/聯系賣家詳細信息的新選項卡。 我正在嘗試檢索所說的 URL,而實際上不必單擊目錄中的每個列表並等待頁面加載,因為這會花費很多時間。

我在 web inspector 中搜索了“href”,但唯一可用的鏈接是每個列表的圖像源。 但是,我注意到單擊每個元素后,都會發送一個 GET 請求方法,這是 URL ( https://api.wallapop.com/api/v3/items/v6g2v4y045ze?language=es ) 它幾乎包含所有我需要的信息,我不確定它是否有用,但這是我得到的最遠的信息。

更新:我嘗試了我被建議的代碼(修改以專門找到可點擊元素中的“href”屬性),但我沒有返回。 我一直在尋找一個“onclick”元素或類似的東西,可能有我正在尋找的東西,但到目前為止,看起來解決方案最終會點擊每個元素並從那里提取所有信息。

elements123 = driver.find_elements(By.XPATH, '//a[contains(@class,"ItemCardList__item")]')
for e in elements123:
    print(e.get_attribute('href'))

我很感激任何見解,提前謝謝你。

你需要這樣的東西:

from selenium import webdriver
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
driver.get("https://google.com")

# Get all the elements available with tag name 'a'
elements = driver.find_elements(By.TAG_NAME, 'a')
for e in elements:
    print(e.get_attribute('href'))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM