【发布时间】:2022-01-04 20:33:33
【问题描述】:
但是,我在访问替代项下的数据时遇到了困难(注意:并非所有人都有替代项)。有替代品的长这样:
示例在链接的第 2 页
请帮我完成代码,我必须收集替代品的零件号。
这是我的代码:
from selenium import webdriver
from time import sleep
import csv
# initializing webdriver
driver = webdriver.Chrome(executable_path="~~chromedriver.exe")
url = "https://datacentersupport.lenovo.com/gb/en/products/storage/fibre-channel-switches/b6505-fc-san-switch/3873/parts/display/compatible"
driver.get(url)
sleep(5)
#getting breadcrumbs
bread1 = driver.find_element_by_xpath("//span[@class='prod-catagory-name']")
bread2 = driver.find_element_by_xpath("//span[@class='prod-catagory-name']/a")
#grabbing table data and navigating
pages = int(driver.find_element_by_xpath("//div[@class='page-container']/span[@class='icon-s-right active']/preceding-sibling::span[1]").text)
num = pages -1
for _ in range(num):
rows = driver.find_elements_by_xpath("//table/tbody/tr/td[2]/div")
for row in rows:
parts = row.text
with open(filename, 'a', encoding='utf-8') as file:
file.write(url + "," + bread1.text + "," + bread2.text + "," + parts + "\n")
pagination = driver.find_element_by_xpath("//div[@class='pagecontainer']/span[@class='icon-s-right active']").click()
sleep(5)
driver.close()
如果我需要更改或修改代码以获得替代品,请告诉我。
【问题讨论】:
标签: python selenium selenium-webdriver css-selectors webdriverwait