【问题标题】:Using Selenium to click specific links in a table使用 Selenium 点击表格中的特定链接
【发布时间】:2022-01-04 20:33:33
【问题描述】:

我必须从这个链接收集数据:https://datacentersupport.lenovo.com/gb/en/products/storage/fibre-channel-switches/b6505-fc-san-switch/3873/parts/display/compatible

但是,我在访问替代项下的数据时遇到了困难(注意:并非所有人都有替代项)。有替代品的长这样:

示例在链接的第 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


    【解决方案1】:

    要访问webpage 中的替代项下的数据,您需要为visibility_of_all_elements_located() 扩展诱导WebDriverWait替代项,您可以使用以下Locator Strategies 之一:

    • 代码块:

      driver.get("https://datacentersupport.lenovo.com/gb/en/products/storage/fibre-channel-switches/b6505-fc-san-switch/3873/parts/display/compatible")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.icon-select-down"))).click()
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., '20')]"))).click()
      for element in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "span[class='icon-s-down']"))):
          element.click()
          print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@class='icon-s-up']//following::tr[3]/td[contains(@class,'enabled-border')]//div[text()]"))).text)
      
    • 控制台输出:

      Lenovo 30m LC-LC OM3 MMF Cable
      Lenovo 30m LC-LC OM3 MMF Cable
      5m LC-LC OM3 MMF Cable
      5m LC-LC OM3 MMF Cable
      

    【讨论】:

    • 谢谢。那么知道如何将其合并到我的原始代码中吗?因为我需要包括替代品在内的所有细节......
    • 谢谢,我找到了将其合并到我的代码中的方法。
    • 我把它放在了 try-except 块中
    • @Reggie18 很高兴能为您提供帮助。 Vote up questions and answers 你觉得很有帮助。见Why is voting important
    • 谢谢。不过小问题。我无法将它们导出到 csv 文件。鉴于我发布的所有代码,如果每行中有多个替代品,我如何将数据导出到 csv 文件。喜欢这个链接 datacentersupport.lenovo.com/gb/en/products/storage/…>
    猜你喜欢
    • 2016-09-23
    • 2020-09-10
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    相关资源
    最近更新 更多