![](/img/trans.png)
[英]find html element, without proper class name or id, with javascript
[英]Selenium - Identifying an HTML element without an ID or CSS class name
我正在Python 3中使用Selenium WebDriver來抓取此網頁 / 相關HTML(Pastebin),並試圖在“出發航班” div中獲取價格清單。 該div沒有ID,也沒有CSS類名,因此我嘗試通過XPath進行標識。 在Pastebin上,找到我正在談論的div,請按Ctrl + F <div class="">
。
這是我的代碼:
outbound_fares = browser.find_elements_by_xpath("//*[@id='air-booking-product-0']/div[4]")
outbound_prices = outbound_fares.find_elements_by_class_name("currency_dollars")
第一行應標識div,第二行應獲取該div中所有價格的列表。 但是,我發現outbound_prices
為空。 我的猜測是第一行是錯誤的,但是我不確定如何正確識別div。 謝謝。
您可以使用此cssSelector並從出發航班獲取所有價格:
cssSelector:
div[class='fare-button--hybrid'] span>span[class*='screen-reader']
試試這個cssSelector,讓我知道狀態。 如果您對此有任何疑問,請告訴我。
您想要的元素沒有ID,但是我不知道您的意思是“沒有CSS選擇器”。 另一個定位器可能更有效地找到它,等等,但是頁面上的每個元素都可以通過CSS選擇器找到。
您可以使用此CSS選擇器返回頁面上的所有價格。
span.fare-button--value-total
以下是一些參考資料,供您閱讀以了解有關CSS選擇器以及如何使用它們的更多信息。
https://www.w3.org/TR/selectors/#overview
https://saucelabs.com/resources/articles/selenium-tips-css-selectors
https://www.smashingmagazine.com/2009/08/taming-advanced-css-selectors/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.