簡體   English   中英

python中的硒通過相對xpath查找元素

[英]selenium in python finding an element via relative xpath

我正在嘗試瀏覽 PADI 船宿頁面以獲取一些船只、出發日期和價格信息。 我能夠從 chrome 調試控制台獲取 xpath 並讓 selenium 找到它。 但我想通過使用相對路徑使它更好,但我不知道該怎么做。 這是我到目前為止得到的:

from selenium import webdriver
import pdb

browser = webdriver.Chrome()


browser.get('https://travel.padi.com/s/liveaboards/caribbean/')
assert 'Caribbean' in browser.title


elem2 = browser.find_elements_by_xpath('//*[@id="search-la"]/div/div[3]/div/div[2]/div[3]/div')

print(elem2)
print(len(elem2))

browser.close()

因此,正如您所看到的,代碼將轉到 PADI,找到每艘潛水船的所有卡片,然后在列表中將其還給我。 此處使用的 xpath 來自最近的可用 id,但從那時起,它都是絕對路徑 div/div/div 等。我想知道是否可以以某種方式將其更改為相對路徑。

謝謝。

您應該使用class和/或id來縮短xpath

當您找到cards您可以使用帶有以./開頭的xpath每張卡片 - 因此它將是相對於該元素的xpath並且它只會在該元素內搜索。

您還可以在xpath任何部分使用//來跳過一些不重要的標簽。

您可以將其他find_element_by_find_elements_by_card find_elements_by_使用,它也只會在此元素內搜索 - 因此它將是相對的。

import selenium.webdriver

driver = selenium.webdriver.Chrome() # Firefox()

driver.get('https://travel.padi.com/s/liveaboards/caribbean/')

all_cards = driver.find_elements_by_xpath('//div[@class="boat search-page-item-card "]')

for card in all_cards:
    title = card.find_element_by_xpath('.//a[@class="shop-title"]/span')
    desc  = card.find_element_by_xpath('.//p[@class="shop-desc-text"]')
    price = card.find_element_by_xpath('.//p[@class="cur-price"]/strong/span')

    print('title:', title.text)
    print('desc:',  desc.text)
    print('price:', price.text)

    all_dates = card.find_elements_by_css_selector('.cell.date')

    for date in all_dates:
        day, month = date.find_elements_by_tag_name('span')
        print('date:', day.text, month.text)

    print('---')

示例結果(您可以使用不同貨幣的價格)

title: CARIBBEAN EXPLORER II
desc: With incredible, off-the-beaten path itineraries that take guests to St Kitts, Saba and St Maarten, this leading liveaboard spoils divers with five dives each day, scenic geography and a unique slice of Caribbean culture.
Dates do not match your search criteria
price: PLN 824
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---
title: BAHAMAS AGGRESSOR
desc: Featuring five dives a day, the well-regarded Bahamas Aggressor liveaboard is the ideal choice for divers who want to spend as much time under the water as possible then relax in an onboard Jacuzzi.
Dates do not match your search criteria
price: PLN 998
date: 7 DEC
date: 14 DEC
date: 21 DEC
date: 28 DEC
---

您需要在 items 中使用classes ./

我只是為你編碼你可以試試!

from selenium import webdriver
import pdb

browser = webdriver.Chrome()

browser.get('https://travel.padi.com/s/liveaboards/caribbean/')

items = browser.find_elements_by_xpath('//div[@class="boat-info"]')

for item in items :
    title = item.find_element_by_xpath('.//a[@class="shop-title"]/span')
    description = item.find_element_by_xpath('.//p[@class="shop-desc-text"]')
    price = item.find_element_by_xpath('.//p[@class="cur-price"]/strong/span')
    print('TITLE: ', title.text)
    print('DESCRIPTION: ', description.text)
    print('PRICE: ', price.text)
    print('------------------NEW-RECORD------------------------')

暫無
暫無

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

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