Приведенный ниже код вводит серийный номер производителя, HL4RPV-50
, в конце концов, я хотел бы перейти к списку номеров деталей и вернуть его информацию.
На данный момент я тестирую код для нескольких входов, жестко закодированных. При вводе одной детали HL4RPV-50
я получаю чистый вывод без ошибок.
Когда я жестко кодирую деталь FSJ4-50B
, я получаю
ошибку
import time
#Need Selenium for interacting with web elements
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
#Need numpy/pandas to interact with large datasets
import numpy as np
import pandas as pd
import itertools
chrome_path = r"C:\Users\James\Documents\Python Scripts\jupyterNoteBooks\ScrapingData\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://www.tessco.com/login")
userName = "FirstName.SurName321123@gmail.com"
password = "PasswordForThis123"
#Set a wait, for elements to load into the DOM
wait10 = WebDriverWait(driver, 10)
wait20 = WebDriverWait(driver, 20)
wait30 = WebDriverWait(driver, 30)
elem = wait10.until(EC.element_to_be_clickable((By.ID, "userID")))
elem.send_keys(userName)
elem = wait10.until(EC.element_to_be_clickable((By.ID, "password")))
elem.send_keys(password)
#Press the login button
driver.find_element_by_xpath("/html/body/account-login/div/div[1]/form/div[6]/div/button").click()
#Expand the search bar
searchIcon = wait10.until(EC.element_to_be_clickable((By.XPATH, "/html/body/header/div[2]/div/div/ul/li[2]/i")))
searchIcon.click()
searchBar = wait10.until(EC.element_to_be_clickable((By.XPATH, '/html/body/header/div[3]/input')))
searchBar.click()
#load in manufacture part number from a collection of components, via an Excel file
#When I search for HL4RPV-50 I get results returned no problem
#When I search for FSJ4-50B, the price xpath can't be found
searchBar.send_keys("HL4RPV-50" + '\n')
# wait for the products information to be loaded
products = wait30.until(EC.presence_of_all_elements_located((By.XPATH,"//div[@class='CoveoResult']")))
# create a dictionary to store product and price
productInfo = {}
# iterate through all products in the search result and add details to dictionary
for product in products:
# get product info such as OEM, Description and Part Number
productDescr = product.find_element_by_xpath(".//a[@class='productName CoveoResultLink hidden-xs']").text
mfgPart = product.find_element_by_xpath(".//ul[@class='unlisted info']").text.split('\n')[3]
mfgName = product.find_element_by_tag_name("img").get_attribute("alt")
# get price
price = product.find_element_by_xpath(".//div[@class='price']").text.split('\n')[1]
print(price)
# add details to dictionary
productInfo[mfgPart, mfgName, productDescr] = price
print()
print()
for (mfg_part, mfg_OEM, description), price in productInfo.items():
mfg_id = mfg_part.split(': ')[1]
if mfg_id == 'HL4RPV-50':
print('Part #:', mfg_id)
print('Company:', mfg_OEM)
print('Description:', description)
print('Price:', price)
print()
print()
#time.sleep(5)
driver.close()
Выход для HL4RP-50:
$1.89
$1.89
$19.94
$39.26
$32.99
$33.33
$42.82
$20.30
Part #: HL4RPV-50
Company: CommScope
Description: 1/2" Plenum Air Cable, Off White
Price: $1.89
Выход для FSJ4-50B:
$85.68
$2.78
$115.51
---------------------------------------------------------------------------
NoSuchElementException Traceback (most recent call last)
<ipython-input-112-92a2ced2ae46> in <module>
59
60 # get price
---> 61 price = product.find_element_by_xpath(".//div[@class='price']").text.split('\n')[1]
62 print(price)
63
Почему я получаю сообщение об ошибке при поиске другой детали? Я проверил XPATH, и они одинаковы.