簡體   English   中英

無法在 Selenium 和 Python 中使用 OR 來定位元素

[英]Unable to locate elements using OR in Selenium with Python

I want to scrape this web page using selenium in Python: https://www.lelo.com/es/juguetes-sexuales-para-parejas .

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import pandas as pd
from selenium.webdriver.common.action_chains import ActionChains
import time
from tqdm import tqdm
from selenium.common.exceptions import NoSuchElementException
driver.get('https://www.lelo.com/es/juguetes-sexuales-para-parejas/')

我使用以下代碼僅識別了此頁面上的可見鏈接:

masa_perso_flist = driver.find_elements_by_xpath('//div[@class="views-field views-field-rendered- 
entity"]') 
filtered_links = [link for link in masa_perso_flist if link.is_displayed()]

listOflinks = []
for masa in filtered_links:
    ppp1=masa.find_element_by_tag_name('div')
    ppp2=masa.find_element_by_tag_name('a')
    listOflinks.append(ppp2.get_property('href'))

對於每個產品,我從 listOflinks 打開鏈接並嘗試提取每個產品的名稱、描述、價格、評論數量和平均評論。 我發現用於捕獲我感興趣的信息的產品頁面中的元素並不相似。例如,在名稱和描述的情況下,有兩種可能的路徑(XPath)來提取信息,我已經成功地做到了。 但是,我正在努力捕捉價格。 在價格的情況下,我嘗試使用此代碼:

alldetails = []
for i in tqdm(listOflinks):
    driver.get(i)
    try:
        Precio = driver.find_element_by_xpath('.//td[@class= "price-amount"] |.//table[@class= "price-amount"]').text
        # I also tried: Precio = driver.find_element_by_xpath('.//td[@class= "price-amount"] |.//tr[@class="price-label"]').text   
    except NoSuchElementException:
            Precio = ("No prices")
    tempJb = {'Precios': Precio}
    alldetails.append(tempJb)
    print(alldetails)

這是我的 output:

[{'Price': '169.00 USD'}, {'Price': ''}, {'Price': ''}, {'Price': ''}, {'Price': ''}, {'Price': ''}]

如果我的代碼錯誤,為什么我沒有收到錯誤消息? 為什么我得到 {'Price': ''} 而不是 {'Price': 'No prices'} 可能這是一個愚蠢的問題,但我真的很感謝你在我學習為這種情況下開發適當的代碼時提供的幫助。 我嘗試了多種 XPath 組合來捕獲價格信息,但我的目的仍然失敗。 非常感謝。

使用 get_attribute('textContent') 嘗試以下操作

get_attribute('textContent') vs.text

如果數據被隱藏或其他方式,將獲取數據。

driver.get('https://www.lelo.com/es/juguetes-sexuales-para-parejas/')
masa_perso_flist = driver.find_elements_by_xpath('//div[@class="views-field views-field-rendered-entity"]') 
filtered_links = [link for link in masa_perso_flist if link.is_displayed()]

listOflinks = []
for masa in filtered_links:
    ppp1=masa.find_element_by_tag_name('div')
    ppp2=masa.find_element_by_tag_name('a')
    listOflinks.append(ppp2.get_property('href'))
alldetails = []
for i in tqdm(listOflinks):
    driver.get(i)
    try:
        Precio = driver.find_element_by_xpath('.//td[@class= "price-amount"] |.//table[@class= "price-amount"]').get_attribute('textContent')
        # I also tried: Precio = driver.find_element_by_xpath('.//td[@class= "price-amount"] |.//tr[@class="price-label"]').text   
    except NoSuchElementException:
            Precio = "No prices"
    tempJb = {'Precios': Precio}
    alldetails.append(tempJb)
    print(alldetails)

我沒有 tqdm 但 output 看起來是正確的。

輸出:

[{'Precios': '$229.00'}]
[{'Precios': '$229.00'}, {'Precios': '$539.00'}]
[{'Precios': '$229.00'}, {'Precios': '$539.00'}, {'Precios': '$219.00'}]
[{'Precios': '$229.00'}, {'Precios': '$539.00'}, {'Precios': '$219.00'}, {'Precios': '$209.00'}]
[{'Precios': '$229.00'}, {'Precios': '$539.00'}, {'Precios': '$219.00'}, {'Precios': '$209.00'}, {'Precios': '$249.00'}]
[{'Precios': '$229.00'}, {'Precios': '$539.00'}, {'Precios': '$219.00'}, {'Precios': '$209.00'}, {'Precios': '$249.00'}, {'Precios': '$259.00'}]

暫無
暫無

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

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